• react老项目 升级react-router


    升级react-router 3.2.1,先卸载旧的router,安装6.0+版本的router

    1. yarn remove react-router
    2. yarn add react-router-dom react-router

    createBrowserRouter 要增加basename的话加在数组后边

    1. const Routes = createBrowserRouter([......],{
    2. basename: '/app'
    3. })

    component 改为 element,且element需为reactNode 

    改之前

    1. import B from './b'
    2. {
    3. path: '/a(/:id)',
    4. component: B,
    5. }

    改之后,  同时不再支持可选路径 , 坑爹,很多人都说坑爹 V6: Optional parameters · Issue #7285 · remix-run/react-router · GitHub

    1. {
    2. path: 'a',
    3. element: <B/>,
    4. children: [{
    5. path: ':id',
    6. element: <B/>
    7. }]
    8. },

    this.context.router.listenBefore  listenBefore也已经被弃用 

    1. componentDidMount() {
    2. this.routerChangeListener = this.context.router.listenBefore(this.beforeRouteChange);
    3. }
    4. componentWillUnmount() {
    5. this.routerChangeListener && this.routerChangeListener();
    6. }
    7. beforeRouteChange = () => {
    8. try {
    9. this.props.closeLightBox();
    10. } catch (error) {
    11. console.log('beforeRouteChange', error);
    12. }
    13. return true;
    14. };

    更改为

    1. componentDidMount() {
    2. this.routerChangeListener = this.beforeRouteChange
    3. }
    4. shouldComponentUpdate(props) {
    5. if (props.location.search !== this.props.location.search) {
    6. this.routerChangeListener();
    7. }
    8. }
    9. componentWillUnmount() {
    10. this.routerChangeListener && this.routerChangeListener();
    11. }
    12. beforeRouteChange = () => {
    13. try {
    14. this.props.closeLightBox();
    15. } catch (error) {
    16. console.log('beforeRouteChange', error);
    17. }
    18. return true;
    19. };

    location.action === "PUT"  location.action已被弃用了,暂时找不到解决方案,如果有小伙伴知道如何实现该功能,请评论区告诉我

    history={rbHistory} 该方法已弃用createBrowserRouter自带history

    onUpdate方法弃用,于是在最外层的组件上加了个方法, componentDidUpdate 的时候执行

    1. const rbRoutes = createBrowserRouter([
    2. {
    3. path: '/',
    4. element: <Layout onUpdate={onRouteUpdate } />,
    5. children: [...]
    6. }
    7. )

    1. componentDidUpdate() {
    2. this.props.onUpdate(this.props)
    3. }

     location.query 不见了,需要自己处理

    导航相关的,由于props不再有router对象,为了项目不进行大改,需要自己包装个方法

    1. import React from 'react';
    2. import {
    3. useLocation,
    4. useNavigate,
    5. useParams,
    6. useFormAction,
    7. } from "react-router-dom";
    8. import queryString from 'query-string';
    9. export function withRouter(Component) {
    10. function ComponentWithRouterProp(props) {
    11. let location = useLocation();
    12. let navigate = useNavigate();
    13. let params = useParams();
    14. const query = queryString.parse(location.search);
    15. const handleNavigate = (url, isReplace) => {
    16. let handleUrl = url;
    17. if (handleUrl[0] !== '/') {
    18. handleUrl = '/' + handleUrl;
    19. }
    20. navigate(handleUrl, { replace: isReplace });
    21. };
    22. const router = {
    23. push: (url) => handleNavigate(url),
    24. replace: (url) => handleNavigate(url, true),
    25. goBack: () => navigate(-1),
    26. location: { ...location, action: useFormAction() },
    27. query
    28. };
    29. return (
    30. <Component
    31. {...props}
    32. location={{ ...location, query }}
    33. navigate={navigate}
    34. params={params}
    35. router={router}
    36. />
    37. );
    38. }
    39. return ComponentWithRouterProp;
    40. }
    41. export function withRouterDom(Component) {
    42. const Node = withRouter(Component);
    43. return <Node />;
    44. }
    1. - import PropTypes from 'prop-types';
    2. + import { withRouter } from 'scripts/utils/RBWithRouter';
    3. - this.context.router.goBack();
    4. + this.props.router.goBack();
    5. - Index.contextTypes = {
    6. - router: PropTypes.object.isRequired
    7. - };
    8. - export default Index;
    9. + export default withRouter(Index);

  • 相关阅读:
    【深度学习】特征融合的重要方法 | 张量的拼接 | torch.cat()函数 | torch.add(函数
    Java面试题 每日五道 冲刺面试
    【android】View的事件体系3-弹性滑动
    哪个运动耳机比较好?适合运动佩戴的运动耳机推荐
    旋转链表-双指针思想-LeetCode61
    bert ranking listwise demo
    Matlab-SSVEP的深度学习应用
    负载均衡架构
    Linux端口及端口监听
    WZOI-256奇怪的数列
  • 原文地址:https://blog.csdn.net/LIUzhenjie_/article/details/127639607