In React, the render
method is used to render a React component or element into the DOM (Document Object Model). It is commonly used with the ReactDOM
library to render components into a specific container element in the HTML document.
Here’s an example of how the render
method is used:
import React from 'react';
import ReactDOM from 'react-dom';
function App() {
return <h1>Hello, world!</h1>;
}
const rootElement = document.getElementById('root');
ReactDOM.render(<App />, rootElement);
In this example, we have an App
component that renders a heading element. The ReactDOM.render
method is called with the App
component as the first argument and the DOM element retrieved using document.getElementById('root')
as the second argument. This renders the App
component into the specified root element in the DOM.
The render
method is typically called once during the initial setup of the application. React takes care of efficiently updating and re-rendering components as needed when the underlying data or state changes.