2

所以我试图在启动应用程序之前初始化 ExtReact 的存储,这样我就可以加载数据,使用 Redux 存储来更新应用程序的状态。不要被一个 action 的调用弄糊涂了,它只是用来通知 reducer 他现在可以加载 store。为此,我正在关注本教程(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. 我的减速机:

export function usershortcuts(state = initialState, action){
        switch(action.type){
            case types.USER_SHORTCUT_FETCH_DATA_SUCCESS:
                return[...state];
        }
}
  1. 我的初始状态:
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. 配置商店:
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)
    );
}
4

1 回答 1

5

在您的减速器上,您需要返回默认状态,并且您不能将状态保留为未定义,因此您需要将初始状态设置为至少为空值

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
            }
        }
}
于 2018-04-11T12:22:01.333 回答