首页 > 解决方案 > 在 React Native 中重置应用程序或状态

问题描述

我正在使用一个交给我们的应用程序,它相当大,而且我对 React Native 很陌生。

我不确定重置会解决它。但我认为我以某种方式污染了我的状态。一切正常,但后来我想重置我的状态,所以我在app.js

componentWillMount() {
  let persistore = persistStore(store, null, () => {
    this.setState({rehydrated: true})
  });
  // persistore.purge();
}

所以我取消了注释persistore.purge();。但是现在当我尝试启动它时,我得到:

未处理的承诺拒绝:TypeError:null 不是对象(评估“instanceInfo.pcb”)

它不会停止上演。关于我可以尝试做什么的任何想法。要“重置”我所做的事情?我已经在 git 中重置为稳定的提交。

标签: react-native

解决方案


我不明白你的问题重置状态意味着整个商店,即应用程序状态?

如果是的话

const appReducer = combineReducers({
    nav,
    listReducer,
    movieDetail
});

const initialState = appReducer({}, {});

const rootReducer = (state, action) => {  
    if(action.type == "RESET_APP_STATE") {
        state = undefined
    }
    return appReducer(state, action);
}

如果你想要特定的状态,即减速器状态相同的东西,但只针对switch我们使用的那个特定的减速器,return undefined


推荐阅读