【问题标题】:Clear Redux state after dispatching action调度操作后清除 Redux 状态
【发布时间】:2020-07-21 12:59:44
【问题描述】:

在没有任何其他额外库的情况下调度CREATE_ACCOUNT 操作后,从redux-store 清除accountimagename 状态的最简单方法是什么,

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

【问题讨论】:

标签: javascript arrays react-native redux react-redux


【解决方案1】:

最简单的方法是添加另一个 case 来切换,如下所示:

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

      return INITIAL_STATE; 

   default: 
      return state; 
  }
}

现在,如果您将 action.type 设置为“RESET”,您实际上是在清除数据。

【讨论】:

    【解决方案2】:

    如果没有额外的库,我现在不可以,但你可以尝试一下:

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

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多