简体   繁体   中英

Clear Redux state after dispatching action

What is the easiest way to clear the account , image , name state from the redux-store after CREATE_ACCOUNT action dispatched without any other additional library,

import { CREATE_ACCOUNT } from '../actions/meals'

const initialState = {
    account: [],
    image:[],
    name:''
}
const addPerson = (state=initialState, action) =>{
    switch(action.type){
        case CREATE_ACCOUNT:
            const newAccount = new newAccount(
                Date.now().toString(),
                action.accountData.name, 
                action.accountData.image,
                action.accountData.email,
                action.accountData.password
            )
            return {account: state.account.concat(newAccount) }
    default: 
        return state
    }
}
export default addPerson

The easiest way is to add another case to switch like below:

const reducer = (state=INITIAL_STATE, action) {
  switch(action.type) {
    case 'SOME_ACTION_TYPE':
      // some code
    case "RESET":

      return INITIAL_STATE; 

   default: 
      return state; 
  }
}

now if you set action.type to 'RESET' you are actually clearing data.

I dont now if this work without additional library but you may give a try at:

const mapDispatchToProps = (dispatch, { navigation }) => {
  return {
    function: () => dispatch(FirstAction(params)).then(() => dispatch({ type: 'NextActionToDispatch' }))
  };
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM