'How to reset Redux Store using Redux-Toolkit
I am trying to reset current state to initial state. But all my attempts were unsuccessful. How can I do it using redux-toolkit?
const slice = createSlice({
name: 'tweets',
initialState: {
byTweetId: {},
byUserId: {},
allTweetIds: [],
},
// reducers
reducers: {
// reset: (state) => Object.assign(state, initialState),
tweetStoreReseted: (state) => {
//here I need to reset state of current slice
},
},
});
Solution 1:[1]
Create a initial state separately and use that when you reset
const initialState = () => ({
byTweetId: {},
byUserId: {},
allTweetIds: [],
});
const slice = createSlice({
name: 'tweets',
initialState: initialState(),
reducers: {
tweetStoreReseted: state => initialState()
}
});
Solution 2:[2]
This should suffice
const slice = createSlice({
name: 'tweets',
initialState: initialState,
reducers: {
tweetStoreReseted: () => initialState()
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Naren |
| Solution 2 | Carmine Tambascia |
