State not getting updated
I have a project I'm working on currently where I am using zustand for state management. I switched to zustand after encountering similar problems with base state management, but clearly this hasn't helped. I am trying to create a live notification/account update system, where the client uses server-sent events to manage this. The server will send an update every 5 seconds. Now, in the code below, I am very clearly updating the state with this data, but it is not doing anything. Can anyone help me figure out why and help me fix it?
Solution:Jump to solution
Here is what I did:
```typescript
const { notifications, setNotifications } = useNotificationStore();
const [localNotifications, setLocalNotifications] = useState<
Notification[]...
31 Replies
data
outputs the correct data. notifications
and user
do not. These remain the same values as when they are initially set in _app.tsx
Relevant code below:
I am at a loss here and any help would be greatly appreciated
I have been debugging this for the past day and a bit and am getting absolutely nowhereUnknown User•2w ago
Message Not Public
Sign In & Join Server To View
I've already asked numerous AI tools and none of them have been able to help. ChatGPT 4.0, Copilot Chat etc.
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
The above user was unable to help. Anyone else able to provide any insight?
Really stuck here
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
It's just the standard template for zustand stores
So a little something like this
Actually scratch that, will send you the code in a few mins
@Bao @ bhuynh.dev
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
I have tried both
Because I saw a Reddit thread where someone had that, seemed to work for the user stuff
Just for some reason this component doesn't wanna play ball
@Bao @ bhuynh.dev for some further context (sorry for ping), this is the console output:
Where it says
undefined
, this is the output of console.log(notifications)
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
I already have done - here is the updated code
This produces the exact same result
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Right, I get that, but I think we're missing the point here. In my
_app.tsx
, I am happily able to destructure the store and have it work as intended
See here: const { user, setUser } = useUser();
I am destructuring it, yet setUser
works as expected:
It pulls down the correct data - avatar, username, and all other relevant info returned by the API
@Bao @ bhuynh.dev just for the sake of it I have updated my code as requested and am getting the same result.
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
It is being called within
_app.tsx
due to the NotificationProvider
component I have written for creating the actual notification.
SseHandler
must be a child of the provider for it to use the hookUnknown User•2w ago
Message Not Public
Sign In & Join Server To View
Yes, specifically in
SseHandler
it seems any state changes are completely disregarded
As I have also tested this with useUser
and useWarningStore
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Wait a minute.. could it be something to do with SseHandler not returning anything to render?
Just out of curiosity I am going to try moving the functionality to my NavBar component...
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Nope, didn't fix it
Unknown User•2w ago
Message Not Public
Sign In & Join Server To View
Not quite sure what you mean by this?
Are you saying, set it to [] and then set it to [...data]?
@Bao @ bhuynh.dev @FinalDev Hi all, I have completely refactored my code.
Instead of the previous solution, I have moved the SSE functionality back into
_app.tsx
, with a separate Watcher
component that watches for changes to the notifications
store, pushing any notifications marked with shouldNotify
.
Now interestingly, this works to an extent. My notifications get pushed successfully, and what should happen, is on the next pass, all existing notifications should have the shouldNotify
prop marked as false
.
But this interestingly doesn't happen. Instead, if I run a console.log
for notifications
in _app.tsx
, this always returns undefined!!!
What's stranger is that if I, say for example, make a change to my code and save it whilst running in dev mode, the notifications
state updates correctly with console.log
outputting the correct data!
What the hell is going on!?!?!?!
New code in _app.tsx
watcher.tsx
Now, my first thought was that maybe the same thing was happening, where notifications
does not get updated. But if this were the case, then the Watcher
component would not have been able to detect the change!
My understanding of passing a variable into the array for useEffect is that this watches for changes to the value, running the method passed if it changes
I have managed to fix this!
But I am absolutely not happy with the solution.Solution
Here is what I did:
So, to summarize, I have created a separate
localNotifications
state that is not shared between components. This acts as an intermediary store, so that the data can be processed properly, setting shouldNotify
accordingly
Once all notifications have had their relevant props applied, for some reason, setNotifications
then works.