升级到React Router v5,从v4,`onEnter`现在已被弃用。

huangapple go评论70阅读模式
英文:

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 &#39;./containers/Confirmation&#39;;

...

ReactDom.render(
  &lt;Provider store={store}&gt;
    &lt;Router history={history} onUpdate={scrollToTop}&gt;
      &lt;Route exact path={BASKET} component={Main} /&gt;
      &lt;Route path={DELIVERY} component={Main} /&gt;
      &lt;Route path={ADDRESS} component={Main} /&gt;
      &lt;Route path={COMPLETE} component={Confirmation} onEnter={checkOrderComplete(store)} /&gt;
    &lt;/Router&gt;
  &lt;/Provider&gt;,
  document.getElementById(&#39;checkout-target&#39;)
);

...

const checkOrderComplete = (store) =&gt; (nextState, replace) =&gt; {
  const state = store.getState();

  const orderCompleted = state.basket.pubOrderNumber !== &#39;&#39;;

  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 =&gt; {
  const state = store.getState();

  const orderCompleted = state.basket.pubOrderNumber !== &#39;&#39;;

  useEffect(() =&gt; {
    if (!orderCompleted) {
      Logger.error(`incomplete order ➜ redirect to ${BASKET}`);
    }
  }, [orderCompleted]);

  return orderCompleted ? (
    &lt;Route {...props} /&gt;
  ) : (
    &lt;Redirect to={{ pathname: BASKET }} /&gt;
  );
};
ReactDom.render(
  &lt;Provider store={store}&gt;
    &lt;Router history={history} onUpdate={scrollToTop}&gt;
      &lt;Route exact path={BASKET} component={Main} /&gt;
      &lt;Route path={DELIVERY} component={Main} /&gt;
      &lt;Route path={ADDRESS} component={Main} /&gt;
      &lt;CompletedRoute path={COMPLETE} component={Confirmation} /&gt;
    &lt;/Router&gt;
  &lt;/Provider&gt;,
  document.getElementById(&#39;checkout-target&#39;)
);

huangapple
  • 本文由 发表于 2023年6月29日 03:38:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定