How to use the npm library in your react app In ReactJS

Step 1: Create React app:

npx create-react-app my-app
cd my-app

Step 2: Install Your Library

Install your npm library in your React app.

npm install my-react-library

you can find your react library in the node_modules after installation.

node_modules after installation of npm library

Step 3: Use your Library Component:

Javascript




// my-app/src/App.js
import React from 'react';
import Button from 'react-button-library';
 
function App() {
  return (
    <div className="App">
      <Button />
    </div>
  );
}
 
export default App;


Step 10: Run Your React App:

npm start

Output:

final output in app



How to create an npm library from React Components ?

Creating an npm library containing React Components is very simple. In this tutorial, we’ll create a library containing a button component that changes color when clicked. Then we’ll demonstrate how to consume this library in a separate React application.

Similar Reads

Approach to create an npm library from react components:

Create a react app with desired components. Initialize npm in the app Configure Babel and Webpack to compile the components. Set up the npm scripts. Publish your package Install and use the package in other React Apps....

Steps to Create an npm library from react components:

Step 1: Setup Your React Components:...

Folder Structure:

...

Using the npm library in your react app:

...