【问题标题】:Dynamic routes with history.push method使用 history.push 方法的动态路由
【发布时间】:2022-12-16 01:02:54
【问题描述】:

我正在将一些名为 topic 的 URL 状态传递给具有函数名称 goToNewPost 的组件。该函数采用 topic 状态并尝试使用 history.push 方法动态呈现 NewPage 组件。

我的问题是这是否可能,我如何重构它以使路径名与 React 中的 history 对象动态关联。如果不可能,什么是完成这项工作的好方法?

这是将路由到在 topic 状态上传递的新页面的函数:

const gotoNewPost = () => {
        if (
            userContext.userState &&
            userContext.spendableReputation >= unirepConfig.postReputation
        ) {
            // pass topic state to new page
            history.push(
                {
                    pathname: `/${topic}/new`,
                    state: { topic: topic },
                },
                { isConfirmed: true }
            )
        } else {
            console.log(userContext.id)
        }
}

这是包含要呈现的路径名和组件的路由:

<Route component={NewPage} path=":topicId/new" />

【问题讨论】:

    标签: reactjs react-hooks react-router react-router-dom


    【解决方案1】:

    我忘记了路线上path开头的/

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 2021-02-13
      • 2013-01-07
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多