Redux-Toolkit is not updating immidiately after dispatch.
I followed : https://redux-toolkit.js.org/tutorials/quick-start
My code:
quoteSlice.js
export const quotesSlice = createSlice({
name: 'quotes',
initialState,
reducers: {
setQuotes: (state, action) => {
console.log('before: ', current(state));
state.quotes = action.payload;
console.log('after: ', current(state));
},
},
});
slide.js
const swipeHandlerDelete = async key => {
console.log('swipeHandlerDelete:: ' + key);
// await deleteItem(database, key);
const filteredQuote = await quotes.filter(i => i.id !== key);
console.log('Before++');
console.log('filteredQuote.quote: ' + filteredQuote[0].quote);
console.log('quotes.quote: ' + quotes[0].quote);
await dispatch(setQuotes(filteredQuote));
console.log('After++');
console.log('quotes.quote: ' + quotes[0].quote);
};
Result: in Terminal
LOG swipeHandlerDelete:: 1
LOG Before++
LOG filteredQuote.quote: I'm well organized and highly productive
LOG quotes.quote: What is the most valuable use of my Time right now
LOG before: {"quotes": [{"created_at": "2022-07-22 10:10:10", "dd": "2022-07-02", "id": 1, "quote": "What is the most valuable use of my Time right now", "status": 1, "updated_at": ""}, {"created_at": "2022-07-22 10:10:10", "dd": "2022-07-02", "id": 2, "quote": "I'm well organized and highly productive", "status": 1, "updated_at": ""}, {"created_at": "2022-07-22 10:10:10", "dd": "2022-07-02", "id": 3, "quote": "Faith can move mountains", "status": 0, "updated_at": ""}]}
LOG after: {"quotes": [{"created_at": "2022-07-22 10:10:10", "dd": "2022-07-02", "id": 2, "quote": "I'm well organized and highly productive", "status": 1, "updated_at": ""}, {"created_at": "2022-07-22 10:10:10", "dd": "2022-07-02", "id": 3, "quote": "Faith can move mountains", "status": 0, "updated_at": ""}]}
LOG After++
LOG quotes.quote: What is the most valuable use of my Time right now
Here i'm deleting first one. i.e key=0, In this terminal result, before: and after: gets updated. But still its not changing in After++ . Why and how to do this? With this result I have to work further.