Updated April 6, 2023
Introduction to React Component Library
React Component Libraries are developed in order to save development time, react itself is an open-source library developed in JavaScript which helps in the development of the web as well as mobile applications, as we are aware that there are different types of components available, it makes use of those components to save development time as well as easy integration of react app with the given component library.
Syntax:
// import react component
import React from "react";
// import required css file
import "<CSS_File_Name>";
const SimpleTextInput = ({ type = "text", label, value, onChange }) => (
<div className="simple-form-group">
{label &&<label className="simple-text-input-label">{label}</label>}
<input
type={type}
className="simple-text-input"
value={value}
onChange={e =>onChange&&onChange(e.target.value)}
/>
</div>
);
export default SimpleTextInput;
The above syntax shows how to create a component library in react. It first requires importing into our code after we add dependency into our project. In the above syntax, we have defined SimpleTextInput which will be exposed as a library into react native project.
How Does Component Library Work in React?
Please follow the below steps:
1. Create a new project using the below command.
Command:
create-react-app component-library-demo
2. Create a new index.js file inside src folder and make sure you delete all other files from src, the following is the code snippet of how index.js looks like.
Code:
import React from "react";
import ReactDOM from "react-dom";
ReactDOM.render(<div>Welcome to Educba</div>, document.getElementById("root"));
3. Create a new folder lib inside src folder and keep components inside this folder, this folder will be published as a component library when packaged using command npm. Here is an example of how the source code looks like:
Code:
// import react component
import React from "react";
// import required css file
import "./TextInputFormat.css";
const SimpleTextInput = ({ type = "text", label, value, onChange }) => (
<div className="simple-form-group">
{label &&<label className="simple-text-input-label">{label}</label>}
<input
type={type}
className="simple-text-input"
value={value}
onChange={e =>onChange&&onChange(e.target.value)}
/>
</div>
);
export default SimpleTextInput;
4. The above-created component can be exported using the following code snippet:
Code:
import SimpleTextInput from "./SimpleTextInput";
export { SimpleTextInput };
5. In order to test the above component test cases can also be written inside the lib folder, in addition, index.js can be used to test and debug the code before publishing it as a component library. Remember the code written outside src/lib will not be published in the library.
6. Install babel-cli using the below command.
Command:
npmi babel-cli --save-dev
Create a file .babelrc in the project root and add the following:
{
"presets": [["react-app", { "absoluteRuntime": false }]]
}
7. Find package.json and replace the build script with the following content:
Code:
"build": "rm -rf dist&& NODE_ENV=production babel src/lib --out-dirdist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__"
8. Add the below content to package.json before publishing the library:
Code:
"main": "dist/index.js",
"module": "dist/index.js",
"files": [ "dist", "README.md" ],
"repository": {
"type": "git",
"url": "<MENTION URL OF YOUR REPOSITORY>"
}
9. Create a readme file and include steps to install library like the following:
This is a Library of components created using `create-react-app`.For Installation
Run the following command:
Command:
npm install component-library-demo
10. publish the library to npm by running the below command:
Command:
npm run publish
Examples
Now we will see react native code example showing the use of the library. In the src/lib directory source code of file SimpleTextInput is as follows:
Code:
import React from "react";
import "./styles.css";
const SimpleTextInput = ({ type="text", label, value, onChange })=>(
<div className="simple-form-group-example">
{label &&<label className="simple-text-label-example">{label}</label>}
<input
type={type}
className="simple-text-input-format"
value={value}
onChange={e =>onChange&&onChange(e.target.value)}
/>
</div>
);
export default SimpleTextInput;
Source Code of File index.js for Testing of Library is as follows:
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
const root Element = document.getElementById("root");
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement
);
Now App.js where we have Use SimpleTextInput Looks like:
import React from "react";
import "./styles.css";
import TextInput from"./lib";
export default functionApp(){
return(
<div style={{ width:640, margin:"15px auto" }}>
<h1>Welcome to Edubca</h1>
<TextInput label="Email Address"placeholder="[email protected]" />
</div>
);
}
Styles.css file reference above looks like:
.simple-form-group-example {
margin-bottom: 1rem;
}
.simple-text-label-example {
display: block;
color: green;
}
.simple-text-input {
display: inline-block;
margin-bottom: 0.5rem;
font-size: 16px;
font-weight: 400;
color: rgb(38, 40, 41);
}
Output:
Conclusion
The above article provides a clear understanding of the react component library. There are several third-party libraries that can be used to provide more customizations to it
Recommended Articles
This is a guide to React Component Library. Here we discuss the Introduction, syntax, and working of React Component Library along with its example and Code Implementation. You can also go through our other suggested articles to learn more –