2

我打电话时反复收到Could not find router reducer in state tree, it must be mounted under "router"错误消息。dispatch(push("/"));

索引.tsx

const store = configureStore();

ReactDOM.render(
    <Provider store={store}>
        <ConnectedRouter history={history}>
            <Switch>
                <Route exact path="/" component={HomeScreenContainer} />
                <Route exact path="/login" component={LoginScreenContainer} />
            </Switch>
        </ConnectedRouter>
    </Provider>,
    document.getElementById('root'),
);

配置存储.ts

export const history = createBrowserHistory();

export default function configureStore(preloadedState?: any) {
    const store = createStore(
        createRootReducer(history),
        preloadedState,
        composeWithDevTools(applyMiddleware(thunk, routerMiddleware(history))),
    );

    return store;
}

减速器.ts

const createRootReducer = (history: History<any>) =>
    combineReducers({
        router: connectRouter(history),
        login: LoginReducer,
    });

export default createRootReducer;

单击登录表单上的提交按钮后,我会发送给loginAsync操作创建者:

export const loginAsync = (login: LoginCredentials) => {
    return (dispatch: any) => {
        if (login.username != 'me@example.com' || login.password != '1234') {
            dispatch({
                type: SET_LOGIN_ERROR_MESSAGE,
                message: 'Invalid username/password',
            });
        } else {
            console.log('SUCCESS LOGIN');
            dispatch(replace('/')); // ERROR HERE
        }
    };
};

我的控制台显示该SUCCESS LOGIN消息已打印,但上述错误消息紧随其后。

如果我查看 URL,URL 似乎导航到正确的路径“/”,但屏幕仍然显示登录表单而不是我的主页。

任何帮助将不胜感激。

4

0 回答 0