0
我按照示例here嘗試爲我的應用創建基本認證區域 ,這是我原則上非常喜歡的解決方案。這是我index.js
:Redux路由器pushState功能不觸發URL更改
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不會更改。所有相關的道具/狀態等似乎都已到位......是否有我錯過的東西?
感謝