ReactJS render() Method (original) (raw)

Last Updated : 17 Feb, 2025

In React, lifecycle methods manage a component’s behaviour at different stages. The render() method is important for defining the UI, updating it whenever state, props, or context changes, and ensuring the UI stays in sync with the component’s data.

What is the Render() Method in ReactJS?

The render() method is an essential part of React class components that determines what gets displayed on the user interface (UI). It plays a key role in rendering elements and updating the UI dynamically.

**Syntax

class MyComponent extends React.Component { render() { return

Hello, World!

; } }

How do render() Work?

The render() method is called every time React determines that a component’s state or props have changed. When this happens, React re-renders the component, calling the render() method to generate a new version of the UI.

**Here’s a basic example of how the render() method works

JavaScript `

import React, { Component } from 'react'; class Welcome extends Component { render() { return

Hello, {this.props.name}!

; } } export default Welcome;

`

**Output

Screenshot-2025-02-15-173322

React.JS render() Method

**In the example above

**Implementing render() Method

This React class component, Greeting, uses state to conditionally render a welcome message or login prompt based on the user's login status.

JavaScript `

import React, { Component } from 'react';

class Greeting extends Component { constructor(props) { super(props); this.state = { name: 'Anjali', // Initial state isLoggedIn: true, }; }

render() {
    // Conditional rendering based on state
    if (this.state.isLoggedIn) {
        return (
            <div>
                <h1>Welcome back, {this.state.name}!</h1>
                <button onClick={() => this.setState({ isLoggedIn: false })}>Log out</button>
            </div>
        );
    } else {
        return (
            <div>
                <h1>Please log in.</h1>
                <button onClick={() => this.setState({ isLoggedIn: true })}>Log in</button>
            </div>
        );
    }
}

}

export default Greeting;

`

**Output:

Animationkk

Render() Method

**In this example

In React 18, the render() method is replaced by the createRoot. Using the render method in react 18 will show a warning, and your app will work like it's still using React 17, not taking advantage of new features in React 18. To use React 18 features, you should use createRoot() instead of render().

Interesting Fact about the ReactJS render() Method

class MyComponent extends React.Component { render() { return

Hello, Virtual DOM!

; } }

class MyComponent extends React.Component { render() { return ( <>

Title

Description

</> ); } }

render() { this.setState({ count: this.state.count + 1 }); // Wrong!
return

Count: {this.state.count}

; }

**Correct Approach: Use Event Handlers or Lifecycle Methods

componentDidMount() { this.setState({ count: this.state.count + 1 }); }

When setState() or new props are received, render() is called again automatically.

class Message extends React.Component { render() { return

{this.props.text}

; } }

// Changing props will trigger re-render

**Incorrect: Async render() (Will Cause an Error)

async render() { const data = await fetchData(); // Wrong return

{data}

; }

**Correct Approach: Use componentDidMount()

componentDidMount() { fetch("https://api.example.com/data") .then(response => response.json()) .then(data => this.setState({ data })); }

render() in Functional Component (Not exist explicity)

In functional components, the render() method does not exist explicitly. Instead, the component itself is a function that returns JSX. When state or props change, React automatically re-renders the component, and the function is called again to return the updated JSX.

JavaScript `

import React, { useState } from 'react';

const Greeting = () => { const [isLoggedIn, setIsLoggedIn] = useState(true); const [name, setName] = useState('Anjali');

return (
    <div>
        {isLoggedIn ? (
            <>
                <h1>Welcome back, {name}!</h1>
                <button onClick={() => setIsLoggedIn(false)}>Log out</button>
            </>
        ) : (
            <>
                <h1>Please log in.</h1>
                <button onClick={() => setIsLoggedIn(true)}>Log in</button>
            </>
        )}
    </div>
);

};

export default Greeting;

`

**Output:

Animationkk

Functional Component

In this example the render() method is not used, it has returned the JSX directly.

In a functional component, there is no separate "render" method because the function itself acts as the render method and they directly returns the JSX but in the class components we define a render method within the class,

Purpose of render() Method