React Hook does not update on state update
See original GitHub issueHey there, I have been using zustand.js so far but there is a case where the html does not update when the state changes. My Code:
NotesList.js:
import React, { Component, useState } from 'react';
import {useNotes} from '../store.js';
const NotesList = () => {
let notes = useNotes(state => state.data.notes);
let changeCurrentNote = useNotes(state => state.changeCurrentNote);
// let uuidNote = useNotes(state => state.data.notes.uuid.title);
const selectNote = (e) => {
let note_id = e.target.id.split('-')[1];
changeCurrentNote(note_id);
}
return(
<div className="notes">
{
Object.entries(notes).map((note, key) => { // note[0] = id, note[1].content, note[1].title etc
return (<div id={'note-' + note[0]} key={note[0]} onClick={selectNote}>{note[1].title}</div>)
})
}
</div>
)
}
export default NotesList
A part of store.js:
const [useNotes] = create((set, get) => ({
data: {
notes: {
uuid: {
content: "Just a Note.",
title: "A Note here...",
tags: ["Abc"]
},
}
}
}
export {useNotes}
However, if I add let uuidNote = useNotes(state => state.data.notes.uuid.title);
in the Hook the specific title does update (other titles don’t), is there a way to fix that / am I doing someting wrong?
Edit: it works if I use the following save()
function (inside create()
)`
save: () => {
let oldState = get().data.notes;
let currentNote = get().data.currentNote;
oldState[currentNote.id].title = currentNote.title;
oldState[currentNote.id].content = currentNote.content;
oldState[currentNote.id].tags = currentNote.tags;
set(state => {
state.data.notes = null // it does not work if I remove this line
});
set(state => {
state.data.notes = oldState
})
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
The useState set method is not reflecting a change immediately
Component or React.PureComponent , the state update using the updater provided by useState hook is also asynchronous, and will not be reflected immediately....
Read more >React setState does not immediately update the state - Medium
useState React hook. Returns a stateful value, and a function to update it. The function to update the state can be called with...
Read more >Why React doesn't update state immediately - LogRocket Blog
State updates in React are asynchronous; when an update is requested, there is no guarantee that the updates will be made immediately. The ......
Read more >Steps to Solve Changes Not Reflecting When useState Set ...
When the “useState” set method is not reflecting a change immediately, it may be due to the current closure of the state variable....
Read more >Why React setState/useState does not update immediately
The answer: They're just queues ... React this.setState , and useState does not make changes directly to the state object. React this.setState ,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Glad it worked well.
This is not correct. Please do it like below:
Otherwise,
useStore()
with no selector won’t work.Do you mean like this?
It looks a bit verbose.
A little better, but still. I personally don’t think it’s worth it either way…
Thanks.
this should fix.
If you prefer mutating syntax, consider using immer.