未捕获错误:Reducer“usershortcuts”在初始化期间返回undefined

时间:2018-04-11 12:19:25

标签: javascript reactjs react-redux extreact

所以我试图在启动应用程序之前初始化ExtReact的存储,以便我可以使用Redux存储来加载数据,以更新应用程序的状态。不要被动作调用混淆,它只是用来通知减速器他现在可以加载商店。我正在关注本教程(http://docs.sencha.com/extreact/6.5.0/guides/extreact_stores_in_flux.html)。但是我收到了这个错误:

Uncaught Error: Reducer "usershortcuts" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined. 

所以这些是代码中最重要的部分:

  1. index.js(入口点):

    const store = configureStore();
    
    /* Calling of actions. */
    store.dispatch(usershortcutFetchDataThroughStore());
    
      launch(
      <Provider store = {store}>
          <HashRouter>
            <Switch>
    
             {/* <Route path="/" name="Home" component={TextEditor}/>*/}
              <Route path="/" name="Home" component={Full}/>
            </Switch>
          </HashRouter>
      </Provider>
    );
    
  2. 我的减速机:

  3. export function usershortcuts(state = initialState, action){
            switch(action.type){
                case types.USER_SHORTCUT_FETCH_DATA_SUCCESS:
                    return[...state];
            }
    }
    
    1. 我的初始状态:
    2. export default {
          /* User shortcuts store. */
          userShortCutStore: new Ext.data.Store({
              model: userShortcutModel,
              autoLoad: true,
              pageSize: 10,
              proxy: {
                  type: 'ajax',
                  reader: {
                      type: 'json'
                  },
                  url: '/usershortcut/all/'
              }
          })
      }
      
      1. 配置商店:
      2. export default function configureStore() {
            return createStore(
                rootReducer,
                initialState,
        
                // Apply thunk middleware and add support for Redux dev tools in Google Chrome
                process.env.NODE_ENV !== "production" && window.devToolsExtension ?
                    compose(applyMiddleware(thunk), window.devToolsExtension())
                    :
                    applyMiddleware(thunk)
            );
        }
        

1 个答案:

答案 0 :(得分:3)

在您的reducer上,您需要返回默认状态,并且您也不能将状态保留为undefined,因此您需要将初始状态设置为至少为空值

const initialState = null;
export function usershortcuts(state = initialState, action){
        switch(action.type){
            case types.USER_SHORTCUT_FETCH_DATA_SUCCESS:{
                return[...state];
            }
            default:{
               return state // We return the default state here
            }
        }
}