0

I have a react component that I want to publish to npm and I was hoping someone can explain a few things to help me get started with it. I have published node.js packages on npm in the past, but this is my first react package and I can't get it to work.

This is a small example of what I am planning to achieve:

My package label-wrapper:

import React, { useState, useEffect, useRef, useCallback } from 'react';

export default function ({ label }) {
  return <div className="label-wrapper">{label}</div>
};

Other people that want to use my package:

import React from 'react';
import LabelWrapper from 'label-wrapper';

export default function () {

  return <div><LabelWrapper label="Hello World" /></div>

}
  1. Do I use create-react-app for this? Or do I install react and react-dom manually? Because I want to make it very small that can be imported to other projects. Its kinda like react-router-dom or material-ui but very, very smaller.

  2. If I use create-react-app do I need to build (react-scripts build) also? This is where I am most confused, because create-react-app is for building single page apps and it carries a lot of extras that I don't need.

If you have a link with a thorough guide for this, it would really help.

AnanE
  • 63
  • 1
  • 4
  • https://medium.com/@BrodaNoel/how-to-create-a-react-component-and-publish-it-in-npm-668ad7d363ce hope this article will help you – Rannie Aguilar Peralta Jul 04 '19 at 08:01
  • The article published by @RannieAguilarPeralta is the way to go. An important line is the following: `libraryTarget: 'commonjs2'`, which defines the output specification. If you want to know more about the differences among commonjs, amd and require, please refer to this question: https://stackoverflow.com/questions/16521471/relation-between-commonjs-amd-and-requirejs – Michele Jul 04 '19 at 08:28
  • Thanks for the link! I will check it out. – AnanE Jul 04 '19 at 17:28

0 Answers0