Skip to content
Advertisement

How to get updated redux-toolkit state when component is not re-render

I’m trying to delete item in redux toolkit, but don’t know how, the remove function only work on screen, i have to press twice to delete the previous one

Here is the reducer

JavaScript

Here is the function where i use it: CODE UPDATED

JavaScript

I have to press twice to make it work, for example, i have 4 item, when i press one, the item on screen disappears but the data log still have 4 item, when i click another, it show 3 on console.log but the screen display 2, the redux state is change outside the return() but i can’t capture the updated state, it work the previous one

Here is a gif to show what going on

enter image description here

When i press only one item, it change on UI but when i refresh it return same state

enter image description here

When i click twice or more, it make changes to previous enter image description here

Updated

The redux-persist code:

JavaScript

I also added the useEffect by this, but problem is when i log the changes in function, it remain the same:

enter image description here

Advertisement

Answer

here is how you can log updated data correctly, as state update is asynchronous it doesn’t change immediately when you dispatch removeNote

JavaScript

about reloading issue, try to close the app and open it like a user of your app would (minimize the app -> remove the app from recently opened apps -> open app again ) , instead of reloading the project.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement