I am trying to use context API object in my component and update the same context object twice (once during initial component load and other on cell click)
For some reason, when I update it on cell click and console out the value, it still has the earlier value i.e. the earlier value set during load. Why is this?
My expectation is that context object should get overwritten entirely on cell click with the new values.
Below is my relevant code;
const [data, setData] = React.useContext(DataContext);
useEffect(() => {
if (props.someThing) {
setData({
attr1: '1',
attr2: '2'
})
}
}, [data.attr1]);
const onCellClick = (e) => {
console.log(e);
if (data.attr1 === '1') {
setData({
newAttrPostClick1: '3',
newAttrPostClick4: '4'
});
console.log("data : " + JSON.stringify(data)); // This is printing {attr1: '1',attr2: '2'} and not {newAttrPostClick1: '3',newAttrPostClick4: '4'}
history.push("/SomePage");
}
}