EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

React Component Library

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » React Native Tutorial » React Component Library

React Component Library

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:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

// 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"));

Popular Course in this category
React JS Redux Training (1 Course, 5 Projects)1 Online Courses | 5 Hands-on Projects | 18+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,487 ratings)
Course Price

View Course

Related Courses

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:

React Component Library Example 1

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 –

  1. React Native AsyncStorage
  2. React Native Progress Bar
  3. React Native Libraries
  4. React Native Drawer Navigation

React JS Redux Training (1 Course, 4 Projects)

1 Online Courses

5 Hands-on Projects

18+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
React Native Tutorial
  • Basic
    • What is React Native?
    • React Versions
    • React Constructor
    • React Native Architecture
    • React Native Libraries
    • React Components Libraries
    • React Native Components
    • React Component Library
    • React Component Lifecycle
    • React Native Framework
    • React Higher Order Component
    • React Ternary Operator
    • React Native Charts
    • React Native Layout
    • React Native Grid
    • React Native Fetch
    • React Native Modal
    • React Native SVG
    • Button in React Native
    • React List Components
    • React Native Element
    • React Native FlatList
    • React Native SectionList
    • react native dropdown
    • React Native Menu
    • React Native State
    • React State Management
    • React Native Tabs
    • React Native Tab Bar
    • React Format
    • React-Native Switch
    • React Native Firebase
    • React Native Flexbox
    • React Native StatusBar
    • React Native ScrollView
    • React Native ListView
    • React Native TextInput
    • React Native Table
    • React-Native Border Style
    • React Native Search Bar
    • React-Native StyleSheet
    • React Native Vector Icons
    • React Native Login Screen
    • React Native Splash Screen
    • React Native Image Picker
    • React Native Navigation
    • React Native Swift
    • React Controlled Input
    • React Fragment
    • React Native Color
    • React Portals
    • React Refs
    • React shouldComponentUpdate()
    • React ComponentDidMount()
    • React componentWillUpdate()
    • React Native Orientation
    • React Native Animation
    • React Native Form
    • React Props
    • React Native Linear Gradient
    • React Native AsyncStorage
    • React Error Boundaries
    • React Native Progress Bar
    • React-Native Calendar
    • React Native Linking
    • React Native DatePicker
    • React Native Image
    • React Native Drawer
    • React Native Drawer Navigation
    • React Native Fonts
    • React Native Overlay
    • React Native OneSignal
    • React Native Template
    • React Native Router
    • React Router Transition
    • React Dispatcher
    • React Native Redux
    • React JSX
    • React native keyboardavoidingview
    • React Native Permissions
    • React Redux Connect
    • React Native Material
    • React Native Gesture Handler
    • React Native Theme
    • React Native Accessibility
    • React Native Justify Content
    • MobX React Native
    • React Native Authentication
    • React Native UUID
    • React Native Geolocation
    • React Native Debugger
    • React Native Carousel
    • React Native Local Storage
    • React Native TypeScript
    • React Bootstrap
    • React Native SQLite
    • React Native Interview Questions
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - React JS Redux Training (1 Course, 4 Projects) Learn More