【发布时间】: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