javascript - 如何在React-router中找到Popup状态和刷新状态?

标签 javascript reactjs react-router

我知道这个问题有点愚蠢,但我对这个网站的产品狩猎他们是如何做到这一点感到非常困惑。当使用 react 路由器单击产品列表弹出窗口时,就是这样完成的。 enter image description here

但是当我刷新该页面时,它会像这样呈现......这是如何使用 React-router 完成的 enter image description here

最佳答案

我打赌他们会使用 state property当推送页面时,向组件提供有关如何呈现页面的指示。更具体地说,指示它来自哪个组件。例如:

router.push({
  pathname: '/posts/origami-studio-by-facebook',
  state: { fromPosts: true }
})

然后您可以在路由组件中读取路由器的状态来检查要显示的页面。

const Post = (productName) => {
  if(this.context.router.location.state.fromPosts) {
    return <Posts productPopup{productName} /> 
    // open the posts page with a popup for the product
  } else {
    return <PostPage productName={productName} />
  }
}

因此,当您在浏览器中打开页面时,state.fromPosts 未设置,并且您会被重定向到 PostPage。到最后,即使路线相同,看到的却完全不同。

关于javascript - 如何在React-router中找到Popup状态和刷新状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40299440/

相关文章:

javascript - 如何在我的 React 应用程序中导入 JS 库?

javascript - React Router 总是显示 IndexRoute

reactjs - 通用类型 'Component<P, S>' 需要 2 个类型参数

javascript - 即使在事件发生后定义,事件处理程序也会执行

php - 表单在 php 验证之前提交

reactjs - 如何在 Formik 中创建连接/依赖的选择元素?

javascript - 相当于react中的directory、mozdirectory、webkitdirectory?

javascript - React中如何判断当前路由是否活跃

javascript - 将文件分配为隐藏输入的值

javascript - Leaflet Tag Filter Button and MarkerCluster.LayerSupport Plugins - 集成以在单击时更新集群