Do you want to build your own npm reusable react component library. I built this one very quickly. It is a simple component library including of a Card, Button, Form, Table and more components to come. Some key features are the Form component allows you to define form fields as content childeren and the Table component supports sorting, filtering and pagination.
$ npm i hey-react-components
Want to create your own. Follow these steps
Step 1: Set Up Your Project
1. Initialize your project:
mkdir my-component-library
cd my-component-library
npm init -y
2. Install necessary dependencies:
npm install react react-dom
npm install --save-dev typescript @types/react @types/react-dom rollup @rollup/plugin-node-resolve @rollup/plugin-commonjs rollup-plugin-typescript2
3. Set up TypeScript configuration:
Create a tsconfig.json file:
Step 2: Create Your Components
1. Create the folder structure:
mkdir -p src/components/Button src/components/Card
2. Create the Button component:
src/components/Button/Button.tsx
src/components/Button/index.ts
export { default } from './Button';
3. Create the Card component:
src/components/Card/Card.tsx
src/components/Card/index.ts
export { default } from './Card';
4. Create the main export file:
src/index.ts
export { default as Button } from './components/Button';
export { default as Card } from './components/Card';
Step 3: Configure Rollup
1. Create a rollup.config.js file:
Step 4: Build and Publish
1. Get your package.json file right.
2. Build your library:
npm run build
2. Create an npm account if you don't have one already at npmjs.com.
3. Login to npm:
npm login
4. Publish your package:
npm publish --access public
And that's it! Your React component library with a Card and Button component is now published on npm. You can install it in any project using:
npm install your-package-name