【发布时间】:2016-10-14 21:39:41
【问题描述】:
我已经按照示例here 尝试创建一个基本的认证区域
对于我的应用程序,这是我原则上非常喜欢的解决方案。这是我的index.js:
const store = createStore(reducer);
ReactDOM.render(
<Provider store={store}>
<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRedirect to="authenticated" />
<Route path="setup" component={SetupJourney} >
<Route path="details" component={Details}/>
<Route path="account" component={AccountType}/>
</Route>
<Route path="authenticated" component={requireAuthentication(secretPage)} />
</Route>
</Router>
</Provider>,
document.getElementById('root')
);
然后这是我的 AuthenticatedComponent 处理重定向的高阶组件:
export function requireAuthentication(Component) {
class AuthenticatedComponent extends React.Component {
componentWillMount() {
this.checkAuth();
}
componentWillReceiveProps(nextProps) {
this.checkAuth();
}
checkAuth() {
if (!this.props.isAuthenticated) {
this.props.dispatch(pushState(null, '/setup/details', ''));
}
}
render() {
return (
<div>
{this.props.isAuthenticated === true
? <Component {...this.props}/>
: null
}
</div>
)
}
}
const mapStateToProps = (state) => ({
isAuthenticated: state.get('user').get('isAuthenticated')
});
return connect(mapStateToProps)(AuthenticatedComponent);
}
我多年来一直在玩这个,但我无法让组件重定向。在 Redux 开发工具中,我可以看到 @@reduxReactRouter/historyAPI 操作已触发,但 URL 没有改变。所有相关的道具/状态等似乎也已经到位......我错过了什么吗?
谢谢
【问题讨论】:
标签: redux-router