useLocalStorage: Questionable use of useEventCallback #593
dmeehan1968
started this conversation in
General
Replies: 2 comments 1 reply
-
For what it's worth, the use of |
Beta Was this translation helpful? Give feedback.
0 replies
-
yep @dmeehan1968 I came to the same conclusion and also created a PR to address it as mentioned by @seanparmelee. Feel free to nudge @juliencrn if you want to see it merged. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
useLocalStorage
(as derivatives) make use ofuseEventCallback
to make cached versions ofsetValue
andremoveValue
, but these contain derived functions that can change anduseEventCallback
does not support dependencies for regeneration.I've encountered problems in hydration on NextJs around client component pre-render because of this. Changing the
useLocalStorage
code to useuseCallback
with appropriate dependencies seems to resolve this, but I can't speak to the design decisions that led to the use ofuseEventCallback
.In my app, I am storing metadata related to components that will be dynamically generated on the client, and are therefore not available during server-side pre-render. I am using the
{ initialiseWithValue: false }
option, as noted in the docs. I can use theuseLocalStorage
as shipped if I set an empty array for my metadata, but I also want to be able to provide an initial array for when the local storage has no value. When I supply an initial array, I get a 'cannot update a component whilst handling an event' error, but if I modifyuseLocalStorage
to switch touseCallback
instead ofuseEventCallback
, this problem goes away.I'd like some input on whether useEventCallback was intentional here, especially around the lack of dependency checking. I'm not sure I can easily create a working example of the problem as my UI is a little complicated and that might be part of why the problem crops up.
Beta Was this translation helpful? Give feedback.
All reactions