![](/img/trans.png)
[英]this.props.children undefined when using react-router-redux
[英]Cannot read property 'type' of undefined (react-router-redux)
我尝试退出后重定向到该页面。 但是,每次退出时,它都会成功定向页面。 但是,我仍然收到错误
无法读取未定义的属性“类型”
通过“在捕获的异常上暂停”的进一步研究,它与react-router-redux有关 。
因此,以下store.dispatch(push('/signin'))
导致了此问题。 如果我更改为.map(() => ({ type: 'NOT_EXIST' }));
,不会有任何问题。
是什么原因造成的? 谢谢
actions / auth.action.js
export const signOutSucceedEpic = (action$, store) =>
action$
.ofType(SIGN_OUT_SUCCEED)
.map(() => store.dispatch(push('/signin'))); // <- this line causes the issue
动作/ index.js
import { combineEpics } from 'redux-observable';
export default combineEpics(
// ...
signOutSucceedEpic
);
index.js
import { Provider } from 'react-redux';
import { Route } from 'react-router-dom';
import { ConnectedRouter, routerMiddleware, push } from 'react-router-redux';
import createHistory from 'history/createBrowserHistory';
import rootEpic from './actions/index';
const history = createHistory();
const routeMiddleware = routerMiddleware(history);
const epicMiddleware = createEpicMiddleware(rootEpic);
export const store = createStore(
rootReducer,
persistedState,
composeWithDevTools(
applyMiddleware(
epicMiddleware,
routeMiddleware
)
)
);
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
<div>
<Route path="/signin" component={SignIn} />
<Route exact path="/" component={Home} />
</div>
</ConnectedRouter>
</Provider>,
document.getElementById('root')
);
问题是您要在map
运算符中调用store.dispatch
,映射到该store.dispatch()
的返回值,但它不会返回任何内容,因此undefined
的值会由您的史诗发出,然后由redux分派-代表您观察。 然后react-router-redux接收到该undefined
值,但它假定仅将分派具有type
属性的操作,因此会引起问题。
我建议重新检查redux-observable文档,因为直接在您的史诗内部调用store.dispatch
是一种反模式,并且不是必需的。 您的史诗应该发出将由redux-observable分派给您的动作流,因此在这种情况下,您只需删除store.dispatch
映射到push()
动作的结果:
export const signOutSucceedEpic = (action$, store) =>
action$
.ofType(SIGN_OUT_SUCCEED)
.map(() => push('/signin'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.