Good day; I’m in need of assistance with the following problem:
A React class component (extending React.Component
) exposes a method to set it’s state on command. I know that using a prop on the component instead could probably solve this problem, but this public method is extremely helpful in my specific scenario and I’m not willing to give up on it just yet.
The method looks like this:
public setContent = (newDoc: IDocsEntry) => {
console.log('Updating with', newDoc);
this.setState(() => ({doc: newDoc}), () => console.log(this.state.doc));
};
and I’ve also tried
public setContent = (newDoc: IDocsEntry) => {
console.log('Updating with', doc);
this.setState(() => ({newDoc: {id: doc.id, path: doc.path, title: doc.title, content: doc.content}}), () => console.log(this.state.doc));
};
The issue is as following:
- on initially setting this state using this method, everything works as expected
- when I then close this component (it lives in a modal), do something else and then try to update the state using this method, it simply does not update
In my console logs, I can see that the method is called with the new and correct object, but the callback of setState
simply shows the old state from the first invocation of this function.