Home > database >  why components state data are gone in conditional rendering
why components state data are gone in conditional rendering

Time:08-20

I am new to react and come from a background of functional component only.

In my react project,

When I conditionally rendering , ie from false to true, the data inside child component will be gone.

Then I wonder why is that.

Then I heard a concept called unmounting. It means, when my condition change from true to false, the component will get unmounting. And in unmounting, the state inside will gone.

But then, it doesn't add up. Q: Whenever we re-render any other components, just like the normal situation, we will also unmount component in order to do re-rendering. And our state value would not be gone.

Why this problem was happened especially on having conditional statement in react?

Edit:

My emphsis is not on how to avoid state loss. My question is that why data will be gone in conditional rendering. And why unmounts will cause such problem, but re rendering would not cause such ( both also involves unmounting)

Here is my code

In parent:

import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import Child1 from "./child";

import "./styles.css";

function Parent() {
  const [message, setMessage] = useState("initial text");
  const [showChild,setShowChild] = useState(true);
  useEffect(() => {
    console.log("useeffect in parent");
  });

  return (
    <div className="App">
      <button onClick={() => setShowChild(!showChild)}>show child</button>
      {showChild? 
      <Child1 />
      :
      null
      
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);

In child:

import React, { useEffect, useState } from "react";

function Child1() {
  useEffect(() => {
    console.log("useeffect in child");
    console.log("newMessage: "   newMessage);
  });
  const [newMessage, setNewMessage] = useState("");


  return (
    <div>
      <input onChange={(event) => setNewMessage(event.target.value)} />
    </div>
  );
}

export default Child1;

Add some picture to illurste what I mean by data lose in conidtional rendering

  1. enter

https://i.stack.imgur.com/UrIhT.png

click to not show it

https://i.stack.imgur.com/0OC87.png

click to show again

https://i.stack.imgur.com/4zlWk.png

CodePudding user response:

Try moving all state management to the parent component and leave the child component 'dumb'. You can pass the setMessage and any other state variables to the child as props.

Parent:

import React, { useEffect, useState } from "react";
import ReactDOM from "react-dom";
import Child1 from "./child";

import "./styles.css";

function Parent() {
  const [message, setMessage] = useState("initial text");
  const [showChild,setShowChild] = useState(true);
  useEffect(() => {
    console.log("useeffect in parent");
  });

  return (
    <div className="App">
      <button onClick={() => setShowChild(!showChild)}>show child</button>
      {showChild? 
      <Child1 setMessage={setMessage}/>
      :
      null
      
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Parent />, rootElement);

Child:

import React from "react";

function Child1({setMessage}) {

  return (
    <div>
      <input onChange={(event) => setMessage(event.target.value)} />
    </div>
  );
}

export default Child1;

CodePudding user response:

The answer for your question is very simple, While unmounting you are removing the component itself from react-dom. The state, props and all the data's handled inside your component will be active only If the component is inside the react-dom. If the particular component is unmounted, all the states and props that was created and processed will also be removed from react-dom along with the component. And the fresh component, state and props will be mounted in the react-dom if you conditionally render the component again.

  • Related