【问题标题】:React: how to compare path with params witn concrete pathReact:如何将路径与带有具体路径的参数进行比较
【发布时间】:2021-04-27 15:16:14
【问题描述】:

我有 2 个具有不同路径的 App 逻辑部分,我需要知道用户去哪里。我是这样做的:

this.props.history.listen((location, action) =>
    {
        if (action === "PUSH")
        {
            const { modeType } = this.props;

            if ((modeType === first && secondPathes.includes(location.pathname))
                || (modeType === second && firstPathes.includes(location.pathname)))
            {
                // some logic
            }
        }
    });

但它不适用于带有参数的路径:"/customers/1/profile" != "/customers/:id/profile"。如何正确比较路径?

【问题讨论】:

    标签: reactjs path routes comparison


    【解决方案1】:

    解决方案是使用react-router中的matchPath

    matchPath("/customers/3/profile", { path: "/customers/:id/profile" })
    

    并使用some 而不是includes

    【讨论】:

      猜你喜欢
      • 2011-08-25
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      • 2012-02-06
      • 1970-01-01
      相关资源
      最近更新 更多