constA=({ val }: {val?: string})=>{const[num,setNum]=useState(0);useEffect(()=>{setTimeout(()=>{setNum(10);},5000);return()=>{console.log('unmounted');};},[]);return(<><div>{num}</div><div>{val}</div></>);};functionApp(): JSX.Element{const[str,setStr]=useState('');constValA=()=>{return<Aval={str}/>;};return(<><ValA/><buttontype={'button'}onClick={()=>{setStr('changed');}}>change</button></>);}
fail case above image is case for my issue
The current behavior
App show 10 after 5000ms from first render.
if i click change button, it makes unmount A.
The expected behavior
i want A component only rerendering after click change
in diffing algorithm (https://reactjs.org/docs/reconciliation.html), unmount will be triggered by changing tag name. i wonder why A Component is unmounted.
The text was updated successfully, but these errors were encountered:
<ValA /> is remounted every time <App /> renders because it's a local function. React has no way of knowing when different functions are really the same; all it can do is === compare ValA to the previous ValA and see that these are different components. Since ValA doesn't close over anything, it should be moved to an outer scope.
<A /> is remounted every time <App /> rerenders because when reconciliation finds a completely different component type at a particular node (as was the case for ValA here), no attempt is made to reconcile any children of that. In general, if two components are completely different, there's no expectation that the children they produce would be similar and recursively reconciling under them is not a productive use of time.
babel result isReact.createElement(ValA, null). so, is tag name(react reconcile document say ) same with variable name every re-render?
i expect name of variable ValA is used for tag name that diffing algorithm say.
React version: 17.0.2
Steps To Reproduce
Link to code example:
https://codesandbox.io/s/unmount-issue-when-prop-changed-yn12l0
fail case above image is case for my issue
The current behavior
App show
10after 5000ms from first render.if i click change button, it makes unmount A.
The expected behavior
i want A component only rerendering after click change
in diffing algorithm (https://reactjs.org/docs/reconciliation.html), unmount will be triggered by changing tag name. i wonder why A Component is unmounted.
The text was updated successfully, but these errors were encountered: