I have a component children with a form in which the user can put a tittle, description and images.
This component is on a section of it's parent component, and I wan't that if the user changes of section (by error or only looking) it's form data is stored so when he comes back he won't need to write all again.
I thought about storing everything directly on the parent's state, but every time the user presses a key, it would re-render the whole view.
And I thought about creating a variable in the parent, that when the children with the form is rendered, its state will get it's initial state from that parent's variable, and store the information there when the component is about to unmount in useEffect(()=>{return ()=>{ props.formData = formData } },[])
export default class feedClase extends Component {
data = {tittle:"", description: "", Blobs: [] }
render(
<>
<FormChild data={data}/>
<Feed/>
</>
)
}
FormChild({data}){
const [tittle, setTittle] = useState(data.tittle)
const [description, setDescription] = useState(data.description)
const [images, setImages] = useState(data.Blobs)
useEffect(() => {
return () => {
data = {Blobs: images, tittle, description}
//I also tried storing it in the parent's state
//-> props.setData({Blobs: images, tittle, description}) //setData={d=>this.setState({data: d})}
}
}, [])
return(
...
)
}
But it doesn't changes anything in the parent's variables/state. How could I make it work?