I am not sure if the code snippet is stripped-down version of actual code, you struggled with.
For this particular case, there'd be no need to save the prop to the state at all.
Forked Sandbox.
// You can destructure only 👇 needed props constProfile=({name,email})=>{return(<div><p><strong>Name: </strong>{name}</p><p><strong>Email: </strong>{email}</p></div>);};constApp=()=>{// irrelevant code removed for brevityconst[state,setState]=useState({...});consthandleChange=()=>{...};return(<divclassName="App">{/* Pass only "needed" props 👇 /*}<Profilename={state.name}email={state.email}/><buttononClick={handleChange}>Change Profile</button></div>);};
Now the Profile is dumb component, which depends on the props only, which also you can further optimize using useMemo or memo()
Thanks Kim, I understood, this example doesn't make much sense. It was created just to showcase how to sync props to state if there is a need for internal state change without the props change.
Example in a tabbed layout where you already have the data as props, but the current tab has internal data changes which are not used outside. In this case, there is no need to lift the state to parent but can be managed inside the tab and have props as the initial state.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thank you for the post, Paramanatham.
I am not sure if the code snippet is stripped-down version of actual code, you struggled with.
For this particular case, there'd be no need to save the
prop
to the state at all.Forked Sandbox.
Now the
Profile
is dumb component, which depends on the props only, which also you can further optimize usinguseMemo
ormemo()
Thanks Kim, I understood, this example doesn't make much sense. It was created just to showcase how to sync props to state if there is a need for internal state change without the props change.
Example in a tabbed layout where you already have the data as props, but the current tab has internal data changes which are not used outside. In this case, there is no need to lift the state to parent but can be managed inside the tab and have props as the initial state.