This seems to run once as intended, still. This is the result of the console.log() earlier…
As you can see parsedStore !== storage even though I used setStorage(parsedStore)
there is some tricky part about console-logging like this:
from the docs:
The set function only updates the state variable for the next render. If you read the state variable after calling the set function, you will still get the old value that was on the screen before your call.
But , the <Element / > tag doesn’t re-render based on the new storage. The storage variable changes after sometime, as I noticed, but the tags to which it is passed as props doesn’t get re-rendered.
but you are passing setStorage, which is the setter function…
you are not passing state variable it seems like
setStorage is a function, you can probably check it out by console.log in any place of your component, and it will always be the sam
hm, than I don’t know rn
if you are passing storage variable, among others, to the child, it should be all fine there.
Unless there are some possible issues in child components logic
Update. Just recalled some nuance
You mentioned in original post:
I did not see the code of Element component, but
your Main component modifies the storage state
your Element component modifies the storage state
I’ve learned that this is not very good situation and could lead to unexpected behaviour, since the same state ‘has 2 sources of truth’.
Trying to find some reference in docs right now, in case I am confusing some stuff
Check out these videos. I am really sorry to disturb you, but thank you for your help, i have been trying to solve this for hours now. Hopefully, I can solve it with your help…
As you can see in video where useEffect is used, the storage updates as intended but the component is not getting updated.
That’s not the case when useEffect is not used…
I can’t understand why a useEffect that runs once in the code would prevent the component from re-rendering
But how would that cause updating issues? Should i just abstain from using ‘has 2 sources of truth’. and remove the setStorage and maybe use a setter function for the <Element / >?