Avoiding useEffect when data changes
How would you handle data from the server changing, based upon which local state should change too?
The code below "works" but I would like to avoid useEffect, since I feel like it isn't really the right tool for what I want.
So what is the general pattern for how to make changes in server side data affect client side state?
In other words: I have a derived value from the cache that should be able to change on the client side and optionally the user has a post button to update the server side data.
10 Replies
sensitive-blue•3y ago
I'd do this:
rival-blackOP•3y ago
That is actually stunningly simple and I wonder why I didn't think of this myself, thanks!
One thing that is not relevant to my current use case but that I am still wondering about for future cases:
What if we want changes in the server data to also update the feeRate here? Let's say the user updates the fee rate from a different device and we want this update to also be reflected here.
sensitive-blue•3y ago
Split it up into multiple components and pass a key={deviceId}. That will instruct react to reset local state
rival-blackOP•3y ago
Not sure I understand it properly.
I presume splitting it up referred to the data from the query and the local state, so something like:
but where exactly should I pass the key here (presuming my assumption of how to split it up was correct).
sensitive-blue•3y ago
what / where is the
device that changes?
I guess it would be:
rival-blackOP•3y ago
I think I formulated it a bit imprecisely, I'll try to break it down more simply.
The scenario is as follows:
I have a form for updating the fee of a transaction
In that form I have an input field whose value should get initialized to
currentFeeRate + 1
The user should be able to update the value of that inputField (setNewFeeRate)
If the value of currentFeeRate changes, newFeeRate should get updated to that new value
How would I best achieve this without using useEffect?genetic-orange•3y ago
In your example above, if you do:
that should do it. Whenever
currentFeeRate changes, MyChild would be remounted and the newFeeRate state reset to currentFeeRate + 1.
That would erase any change made by the user though.rival-blackOP•3y ago
I don’t think so, because useState would keep the previous value even if props change, but essentially that same idea could work like this I think:
I don’t think I can come up with a better way of doing this though, is there perhaps something more simple that I’m missing?
Otherwise I would mark this question as solved
genetic-orange•3y ago
I don’t think so, because useState would keep the previous value even if props changeThe state would be reset whenever the
key value changes: https://react.dev/learn/preserving-and-resetting-state#option-2-resetting-state-with-a-keyPreserving and Resetting State – React
The library for web and native user interfaces
rival-blackOP•3y ago
Aaah I see, okay that’s cool
In my previous snippet there was no need for useMemo => I edited it to be the simplest one I can think of using the previous approach -> using the different key is even simpler though, thanks so much for your help!!