Components
Components are conceptually similar to JavaScript functions, which are used to split UI into independent, reusable pieces and think about each piece in an isolation. It is easy to maintain and you can update the specific component without affecting the rest of the page. Please refer Components for more details.
Prerequisites
Click here to set up the development environment for React.
Steps involved are given below.
- import React from 'react';
-
- class App extends React.Component {
- render() {
- return (
- <div>
- <FirstName/>
- <LastName/>
- </div>
- );
- }
- }
-
- class FirstName extends React.Component {
- render() {
- return (
- <div>
- <p>First Name: Vijai Anand</p>
- </div>
- );
- }
- }
-
- class LastName extends React.Component {
- render() {
- return (
- <div>
- <p>Last Name: Ramalingam</p>
- </div>
- );
- }
- }
-
- export default App;
- import React from 'react';
- import ReactDOM from 'react-dom';
- import App from './App.jsx';
-
- ReactDOM.render(<App />, document.getElementById('app'));
- <!DOCTYPE html>
- <html lang="en">
-
- <head>
- <meta charset="UTF-8">
- <title>React App</title>
- </head>
-
- <body>
- <div id="app"></div>
- <script src="index.js"></script>
- </body>
-
- </html>
Testing
Run the command given below to start the Server. Open the Browser and type http://localhost:8080/
npm start
Summary
In this blog, you had seen how to create a simple component in React, using ES6.