我正试图在签出后重定向到页面。但是,每次我签出去时,它都会成功地引导页面。但是,我还是得到了错误
无法读取未定义的属性“类型”
通过“暂停捕获异常”的进一步研究,它与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
actions/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')
);
发布于 2017-07-29 20:56:57
问题是在map
操作符中调用map
,映射到该store.dispatch()
的返回值,但它不返回任何内容,因此undefined
值是由epic发出的,然后由redux可观察到的值代表您发出。然后react路由器-redux接收到该undefined
值,但它只假定带有type
属性的操作将被分派,因此它会导致错误。
我建议重新检查可还原的文档,因为在史诗中直接调用store.dispatch
是一种反模式,而不是必要的。您的epic应该会发出一系列的操作,这些操作将通过redux可观察到的方式分派给您,因此在这种情况下,您只需删除store.dispatch
,而是映射到push()
操作的结果:
export const signOutSucceedEpic = (action$, store) =>
action$
.ofType(SIGN_OUT_SUCCEED)
.map(() => push('/signin'));
https://stackoverflow.com/questions/45396343
复制相似问题