打开模式,ReactJS后更新URL

问题描述

我有点反应,我想在用户单击打开模式窗口后更改URL。模态中也有一些数据,我想在URL中反映出来。例如。 “ my.app/folders/project-name /”

我尝试了一些使用React Router的解决方案,但是我似乎并不太正确...

这是我希望可以在打开URL后对其进行更新的模式。

{modalOpen ? (
    <ProjectModal
      projectName={target[0].project_name}
      organization={target[0].organization}
      avatar={target[0].organization_avatar}
      client="xy"
      selectedImg={target[0].images[0]}
      gallery={target[0].images}
      onClose={() => setModalOpen(false)}
    />
  ) : null}

解决方法

确保在路由器中提到了此路由。像这样的东西

<Route path="parent" component="ParentComponent" />
<Route path="parent/modal" component="ParentComponent" />

然后,在设置变量以打开模态时,您将必须使用history.push方法。

history.push('/parent/modal');

希望我的回答有帮助。

签出docs以获得更多信息。