Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset state to initial with redux-toolkit

I need to reset current state to initial state. But all my attempts were unsuccessful. How can I do it using redux-toolkit?

const showOnReviewSlice = createSlice({   name: 'showOnReview',   initialState: {     returned: [],   },   reducers: {     reset(state) {       //here I need to reset state of current slice     },   }, });  
like image 792
Zireael Avatar asked Dec 20 '19 11:12

Zireael


2 Answers

Something like this:

const intialState = {   returned: [] }  const showOnReviewSlice = createSlice({     name: 'showOnReview',     initialState,     reducers: {         reset: () => initialState     } }); 
like image 101
i9or Avatar answered Sep 22 '22 20:09

i9or


This worked for me (mid-late 2020). Formatted with your code context as an example.

const initialState = {   returned: [], };  const showOnReviewSlice = createSlice({   name: 'showOnReview',   initialState,   reducers: {     reset: () => initialState,   }, });  
like image 43
worstestes Avatar answered Sep 23 '22 20:09

worstestes