How React Virtual DOM works when I set both the changed states and the unchanged states

0 votes
With the help of code and example can u tell me How React Virtual DOM works when I set both the changed states and the unchanged states?
Feb 21 in Node-js by Nidhi
• 11,580 points
99 views

1 answer to this question.

0 votes

When you update both changed and unchanged states in React, the Virtual DOM (VDOM) diffing algorithm (Reconciliation) ensures that only the actual differences are reflected in the real DOM.

How It Works?

Render Phase:

React creates a new Virtual DOM tree with the updated state.

It compares the new Virtual DOM with the previous Virtual DOM (diffing process).

Diffing & Optimization:

React skips unchanged states since they don’t affect the UI.

It identifies the minimum number of updates required.

Commit Phase:

Only the changed parts of the real DOM are updated efficiently.

Code Example

import React, { useState } from "react";

const App = () => {

  const [count, setCount] = useState(0);

  const [name, setName] = useState("Alice");

  const updateStates = () => {

    setCount(1); // Changed state

    setName("Alice"); // Unchanged state

  };

  console.log("Component Rendered"); // Observe if it re-renders

  return (

    <div>

      <p>Count: {count}</p>

      <p>Name: {name}</p>

      <button onClick={updateStates}>Update State</button>

    </div>

  );

};

export default App;
Related Question : Force React component rerender without setState
answered Feb 22 by Kavya

Related Questions In Node-js

0 votes
1 answer

How to set the content-type of request header when using Fetch APi?

Hello @kartik, You need to create a fetch ...READ MORE

answered Oct 15, 2020 in Node-js by Niroj
• 82,840 points
8,240 views
0 votes
1 answer

How to Redirect to the Main Page in React JS When Refreshing Other Pages?

To redirect users to the main page ...READ MORE

answered Dec 31, 2024 in Node-js by Navya
111 views
0 votes
1 answer
0 votes
1 answer

How can I use all the React events with Material-UI components?

The best approach is to leverage the ...READ MORE

answered Feb 22 in Node-js by Kavya
42 views
0 votes
1 answer

From php returning JSON to JavaScript

Hii @kartik, You can use Simple JSON for PHP. ...READ MORE

answered Jun 5, 2020 in Java-Script by Niroj
• 82,840 points
1,163 views
0 votes
1 answer

How to use JQuery with ReactJS

Yes, we can use jQuery in ReactJs. ...READ MORE

answered Jun 22, 2022 in Web Development by rajatha
• 7,680 points

edited Dec 15, 2023 by Soumya 14,933 views
0 votes
1 answer

Unsure how to turn this jQuery code to React

You can initialize a state for navTrigger whic tell ...READ MORE

answered Aug 5, 2022 in Web Development by rajatha
• 7,680 points
1,293 views
0 votes
2 answers

How to user Jquery's Plugin "Mondial relay" in react js properly?

Hi, have you found a solution? I'm facing ...READ MORE

answered Nov 30, 2022 in Web Development by Kévin
1,240 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP