Skip to content

onChange calling a parent function: strange behavior; can't read parent state #1713

Answered by joshuaellis
joshon asked this question in Support
Discussion options

You must be logged in to vote

Because the spring memoizes props. To access the new state in app.jsx the function needs to be updated so you can use useCallback and pass lastTimeUpdated as a dependancy. Then in Obj.jsx you pass updateVar as a dependency to make sure that onChange is calling the most up to date version of the function.

This is similar to calling functions in useEffect. You normally get a warning about it because it won't be in the deps and if it is in the deps and not wrapped in useCallback it'll complain again declaring that whenever the function updates (on every render of the component) the effect will fire.

Here's some helpful docs that might explain it better than I did:

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@joshon
Comment options

Answer selected by joshon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants