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 development of web as well as mobile applications, as we are aware that there are different types of components available, react component library 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 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?
In order to create react native component library please follow the below steps:
1. Create a new project using the bellow 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 into the library.
6. Install babel-cli using the below command.
Command:
npmi babel-cli --save-dev
Create a file .babelrc in 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 component 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 to Implement of React Component Library
Now we will see react native code example showing use of its component library. In the src/lib directory source code of file SimpleTextInput is as follows:
Code:
importReactfrom"react";
import"./styles.css";
constSimpleTextInput=({ type="text",label,value,onChange })=>(
<divclassName="simple-form-group-example">
{label &&<labelclassName="simple-text-label-example">{label}</label>}
<input
type={type}
className="simple-text-input-format"
value={value}
onChange={e =>onChange&&onChange(e.target.value)}
/>
</div>
);
exportdefaultSimpleTextInput;
Source Code of File index.js for Testing of Library is as follows:
importReactfrom"react";
importReactDOMfrom"react-dom";
importAppfrom"./App";
constrootElement=document.getElementById("root");
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
rootElement
);
Now App.js where we have Use SimpleTextInput Looks like:
importReactfrom"react";
import"./styles.css";
importTextInputfrom"./lib";
exportdefaultfunctionApp(){
return(
<divstyle={{ width:640, margin:"15px auto" }}>
<h1>Welcome to Edubca</h1>
<TextInputlabel="Email Address"placeholder="name@example.com" />
</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 about the react component library. There are several third-party libraries that can be used to provide more customizations to the component library
Recommended Articles
This is a guide to React Component Library. Here we discuss the Introduction to React Component Library and its Examples along with Code Implementation. You can also go through our other suggested articles to learn more –