-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Post-write callback? #46
Comments
My current solution. Think it's fine?
|
I have a very similar problem. const [tagsState, updateTagsState] = useImmer(tags);
...
const onTagSelectionChanged = useCallback(
(data: any) => {
updateTagsState(draft => {
draft.set(144, { label: 'Some string' });
});
// Here I was expected chages were applied, because updateTagsState has finished
// But I see old Map instance
const tag = tagsState.get(data.id);
if (tag !== undefined && onSelectionChanged !== undefined) {
onSelectionChanged(tag);
}
},
[tagsState, updateTagsState, onSelectionChanged]
); I think the semantic should be - to be able to observe changes immideately after finishing |
Yes, I think useImmer and useImmerReducer's update functions should return new state. |
This is basically not an immer but a react question. The to access state for executing side effects, don't trigger it directly from the rendering, but from const [tagsState, updateTagsState] = useImmer(tags);
...
const onTagSelectionChanged = useCallback(
(data: any) => {
updateTagsState(draft => {
draft.set(144, { label: 'Some string' });
});
},
[tagsState, updateTagsState, onSelectionChanged]
);
// useEffect 'sees' the state that has become the current state for the component
useEffect(() => {
const tag = tagsState.get(data.id);
if (tag !== undefined && onSelectionChanged !== undefined) {
onSelectionChanged(tag);
}
}, [tagState.get(data.id)] |
I'm looking to do something like this, but state isn't updated at this point.
Is there a way to get around this? Or maybe this goes against the philosophy here?
The text was updated successfully, but these errors were encountered: