0

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
Dilhan Bhagat
  • 408
  • 1
  • 10
  • 20

2 Answers2

0

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.

ErfanFi79
  • 36
  • 6
0

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' }))
  };
};