I'm in the process of making a feature to add articles to bookmarks. Now adding to bookmarks works fine, but I don't understand why redux toolkit returns previous state. When I click on the checkbox, I add an object with the article id to the bookmark array. Everything works fine in the redux toolkit, but when you try to check via console.log(), the new object doesn't appear there, but it will be there when another article is added. Each time it returns the previous value
It looks like this:
//Redux state
[
{
id: 15
},
{
id: 5
},
{
id: 21
},
{
id: 4
},
{
id: 33
},
{
id: 27
},
{
id: 28
},
{
id: 30
},
{
id: 19
}
]
//Getting the redux state in the console
[
{
"id": 15
},
{
"id": 5
},
{
"id": 21
},
{
"id": 4
},
{
"id": 33
},
{
"id": 27
},
{
"id": 28
},
{
"id": 30
}
]
Add to bookmarks code
//Slice
const initialState = {
bookmarksData: [],
};
export const bookmarkSlice = createSlice({
name: 'bookmark',
initialState,
reducers: {
addBookmarks: (state, action) => {
state.bookmarksData.push({
id: action.payload.id,
});
}
},
});
//Posts.jsx
const bookmarksData = useSelector((state) => state.bookmark.bookmarksData);
const savedBokmark = ({ id }) => {
dispatch(addBookmarks({ id }));
console.log(bookmarksData);
};
Is there any way to get the current state, not the previous one? I want to send this data to the server, so that the user can save his bookmarks. I don't want to use redux thunk yet
