UseEffect not being called
Hey,
I've got a weird bug in my react component
For some reason, when
mqtt.connected
is changed the useEffect is not called.
For example if i start the broker, it console logs false
and then when i disconnect it console logs true
its almost as if the useEffect is reading the previous state.
However if i change the depedency to be my connected
state variable instead, it updates perfectly....
( was previously wrapping the return in react.memo but i have removed it for this example, as it presents the same issue, the depedency of mqtt?.connected
doesnt get called)18 Replies
the issue is that when
mqtt.connected
is changed, it's not causing the value of mqtt
itself to change, since setMqtt
isn't being called with a new value. setConnected
is updating the connected
state, so it rerenders and triggers the useEffect
right so what would be the best way around this?
continue to just store my own internal connected state (which is kinda of annoying , and makes me have to listen to events)
yeah you'd need to maintain your own state, since u cant just make an immutable copy of the mqtt broker
It's a good candidate for a custom hook actually
hmmmm yea good point
yea coz mqtt is immuntable, so me changing it doesnt actually work...
skipped my mind when i wrote tthis lol
what sort of hook? I can't think of any way for the hook to work?
something like this
you give it options, it returns a client and state
const { client, connected } = useMqtt(...)
wait i didn't update it enoughxd
all good i get the concept,
its very similar to what i already had yea
yeah it's just putting the client logic and state in a little helper
yea
i might just use that directly in the provider instead of a seperate hook.
yeah fairo
would be nice to be able to access
client.connected
without having to export my own state
but yea
it worksyeah since the client isn't designed for react and immutability it won't work
yea
and coz even if i change it to a ref, it wont work in use effects etc
i think the proper way to do this is
useSyncExternalStore
, but you're probs just building an app
yeah using a ref doesn't change much, it's just a bit more correct and communicates the fact that the client won't cause rerenders or be updated with a setter functionyea
ill look into that
i guess i could hook it up like this
that way it will update when state changes and calling
mqtt.connected
should always be correcthmmm i guess but i think relying on the client itself to have the
connected
state be up to date isn't reliableoh wait this causes infinte re renders xd
without a dedicated
useState
, you simply won't get rerenders when the client's state changes
may as well just use the stateyea
for some reason its ifintiely re rendering right now
only reason i know this is because i have
whydidyourerender
plugin
hmm
seems like when the broker is disconnected it continously calls onClose
hmmm
uhh the state
is an object thats why
altho on state change it stil causes that re render which isnt ideal (just doesnt spam it anymore)