当浏览器位置更改时重新呈现 React.js

Rerendering React.js When Browser Location Changes

我有一个使用 React.js 的单页应用程序。查看新内容时,只有页面上的某些元素通过 React 更新。发生这种情况时,我手动执行以下操作(从根位置开始):

window.history.pushState(null, null, "/page2");

更新浏览器位置。但是,当我单击返回时,url 会返回到之前的 url(即“/”),但页面不会更新。

如何监听浏览器导航(back/forward 按钮)的变化并根据更新后的 URL 重新呈现?

您可以使用 react-router

Pete Hunt 使用 director.js(抱歉,不能 post 和 url,因为声誉低),您可以在 ReactJS TodoMVC 示例中看到 here在第 29-34 行。