英文:
upgrading to react router v5 from v4 onEnter is now deprecated
问题
我有一些代码,在其中根据当前路由渲染一些React组件
import Confirmation from './containers/Confirmation';
...
ReactDom.render(
<Provider store={store}>
<Router history={history} onUpdate={scrollToTop}>
<Route exact path={BASKET} component={Main} />
<Route path={DELIVERY} component={Main} />
<Route path={ADDRESS} component={Main} />
<Route path={COMPLETE} component={Confirmation} onEnter={checkOrderComplete(store)} />
</Router>
</Provider>,
document.getElementById('checkout-target')
);
...
const checkOrderComplete = (store) => (nextState, replace) => {
const state = store.getState();
const orderCompleted = state.basket.pubOrderNumber !== '';
if (!orderCompleted) {
Logger.error(`incomplete order ➜ redirect to ${BASKET}`);
replace({ pathname: BASKET });
}
};
onEnter
自升级到v5后不再起作用。如何在升级后的版本中实现相同的功能?
英文:
I have some code where I'm rendering some components in react based off a current route
import Confirmation from './containers/Confirmation';
...
ReactDom.render(
<Provider store={store}>
<Router history={history} onUpdate={scrollToTop}>
<Route exact path={BASKET} component={Main} />
<Route path={DELIVERY} component={Main} />
<Route path={ADDRESS} component={Main} />
<Route path={COMPLETE} component={Confirmation} onEnter={checkOrderComplete(store)} />
</Router>
</Provider>,
document.getElementById('checkout-target')
);
...
const checkOrderComplete = (store) => (nextState, replace) => {
const state = store.getState();
const orderCompleted = state.basket.pubOrderNumber !== '';
if (!orderCompleted) {
Logger.error(`incomplete order ➜ redirect to ${BASKET}`);
replace({ pathname: BASKET });
}
};
onEnter
is no longer working since upgrading to v5. How can I achieve the same functionality with the upgraded version?
答案1
得分: 2
我建议创建一个受保护的路由组件,在挂载时进行检查。这里的思路是,自定义路由访问Redux存储并计算orderCompleted
衍生状态值,根据条件返回路由内容或重定向到购物篮路由。
类似以下代码:
const CompletedRoute = props => {
const state = store.getState();
const orderCompleted = state.basket.pubOrderNumber !== '';
useEffect(() => {
if (!orderCompleted) {
Logger.error(`不完整的订单 ➜ 重定向到 ${BASKET}`);
}
}, [orderCompleted]);
return orderCompleted ? (
<Route {...props} />
) : (
<Redirect to={{ pathname: BASKET }} />
);
};
ReactDom.render(
<Provider store={store}>
<Router history={history} onUpdate={scrollToTop}>
<Route exact path={BASKET} component={Main} />
<Route path={DELIVERY} component={Main} />
<Route path={ADDRESS} component={Main} />
<CompletedRoute path={COMPLETE} component={Confirmation} />
</Router>
</Provider>,
document.getElementById('checkout-target')
);
英文:
I'd suggest creating a protected route component that does the check when it mounts. The idea here is that the custom route accesses the Redux store and computes the orderCompleted
derived state value and conditionally returns the route content or redirects to the basket route.
Something like the following.
const CompletedRoute = props => {
const state = store.getState();
const orderCompleted = state.basket.pubOrderNumber !== '';
useEffect(() => {
if (!orderCompleted) {
Logger.error(`incomplete order ➜ redirect to ${BASKET}`);
}
}, [orderCompleted]);
return orderCompleted ? (
<Route {...props} />
) : (
<Redirect to={{ pathname: BASKET }} />
);
};
ReactDom.render(
<Provider store={store}>
<Router history={history} onUpdate={scrollToTop}>
<Route exact path={BASKET} component={Main} />
<Route path={DELIVERY} component={Main} />
<Route path={ADDRESS} component={Main} />
<CompletedRoute path={COMPLETE} component={Confirmation} />
</Router>
</Provider>,
document.getElementById('checkout-target')
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论