Explain the purpose of render() in ReactJS
React.js library has two components:
- Class components
- Functional Components
Class components uses render function. The ReactDOM.render() function takes two arguments, HTML code and an HTML element.
Purpose of render():
- React renders HTML to the web page by using a function called render().
- The purpose of the function is to display the specified HTML code inside the specified HTML element.
- In the render() method, we can read props and state and return our JSX code to the root component of our app.
- In the render() method, we cannot change the state, and we cannot cause side effects( such as making an HTTP request to the webserver).
Let’s understand the render function with an example.
Creating React Application And Installing Module:
Step 1: Create a React application using the following command.
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command.
Project Structure: It will look like the following.
Now write down the following code in the App.js file.
Step to Run Application: Run the application using the following command from the root directory of the project:
Output: Now open your browser and go to http://localhost:3000/, you will see the following output: