REACT

Update a Component?


Updating a component in React involves modifying its state or props, which triggers a re-render to reflect the changes. Now let’s understand in-depth l how to update components in both functional and class-based React components.

 

I. Functional Components

 

1. Using useState Hook

Functional components use the useState hook to manage state. When the state changes, React re-renders the component.

 

Example: Updating State

import React, { useState } from 'react';
function Counter() {
 // Declare a state variable 'count' with an initial value of 0
 const [count, setCount] = useState(0);

 // Function to update the state
 const handleClick = () => {
   setCount(count + 1);
 };

 return (
   <div>
     <p>You clicked {count} times</p>
     <button onClick={handleClick}>Click me</button>
   </div>
 );
}
export default Counter;

 

Explanation:

  • useState(0) initializes the state variable count to 0.
  • setCount(count + 1) updates the state and triggers a re-render.

 

2. Using useEffect Hook

The useEffect hook lets you perform side effects in functional components, such as fetching data or updating the DOM.


Example: Fetching Data

import React, { useState, useEffect } from 'react';
function DataFetcher() {
 const [data, setData] = useState(null);

 useEffect(() => {
   fetch('/api/data')
     .then(response => response.json())
     .then(data => setData(data));
 }, []); // Empty array means this effect runs once after the initial render

 return (
   <div>
     <p>{data ? JSON.stringify(data) : 'Loading...'}</p>
   </div>
 );
}
export default DataFetcher;


Explanation:
useEffect with an empty dependency array ([]) runs once, similar to componentDidMount.
When data is fetched, setData updates the state and re-renders the component.

 

II. Class Components

Class components manage state using this.state and update it using this.setState.


Example: Updating State

import React, { Component } from 'react';
class Counter extends Component {
 constructor(props) {
   super(props);
   this.state = {
     count: 0
   };
 }

 handleClick = () => {
   this.setState({ count: this.state.count + 1 });
 };

 render() {
   return (
     <div>
       <p>You clicked {this.state.count} times</p>
       <button onClick={this.handleClick}>Click me</button>
     </div>
   );
 }
}
export default Counter;


Explanation:

  • this.state = { count: 0 } initializes the state.
  • this.setState({ count: this.state.count + 1 }) updates the state and triggers a re-render.

 

1. Using Lifecycle Methods

Lifecycle methods allow you to perform actions at different stages of a component's lifecycle.


Example: componentDidUpdate

import React, { Component } from 'react';
class LifecycleComponent extends Component {
 componentDidUpdate(prevProps, prevState) {
   if (prevProps.data !== this.props.data) {
     // Perform some operation when props.data changes
     console.log('Data updated');
   }
 }

 render() {
   return (
     <div>
       <p>{this.props.data}</p>
     </div>
   );
 }
}
export default LifecycleComponent;


Explanation:

componentDidUpdate runs after the component updates, and you can compare prevProps and prevState to decide if further action is needed.

 

III. Handling Props Updates

Props are read-only and passed from parent to child components. When a parent component's state changes, it updates the props of its children, causing them to re-render.


Example: Updating Props

import React, { useState } from 'react';import ChildComponent from './ChildComponent';
function ParentComponent() {
 const [message, setMessage] = useState('Hello');

 const updateMessage = () => {
   setMessage('Hello, World!');
 };

 return (
   <div>
     <ChildComponent message={message} />
     <button onClick={updateMessage}>Update Message</button>
   </div>
 );
}
function ChildComponent({ message }) {
 return <p>{message}</p>;
}
export default ParentComponent;


Explanation:

  • setMessage('Hello, World!'): updates the state in ParentComponent.
  • The new message prop is passed to ChildComponent, causing it to re-render.

 

IV. Conditional Rendering

Sometimes we need to render different content based on certain conditions.

 

Example: Conditional Rendering

import React, { useState } from 'react';
function ConditionalComponent() {
 const [isVisible, setIsVisible] = useState(true);

 const toggleVisibility = () => {
   setIsVisible(!isVisible);
 };

 return (
   <div>
     {isVisible ? <p>Now you see me!</p> : <p>Now you don't!</p>}
     <button onClick={toggleVisibility}>Toggle Visibility</button>
   </div>
 );
}
export default ConditionalComponent;