我必须根据浏览历史实现一些业务逻辑。
我想做的是这样的:
reactRouter.onUrlChange(url => {
this.history.push(url);
});
URL更新后,有什么方法可以接收来自react-router的回调吗?
history.listen()
尝试检测路线变化时,可以使用功能。考虑到您正在使用react- router v4
,请用withRouter
HOC 包装您的组件以访问history
道具。
history.listen()
返回一个unlisten
函数。您将使用它来unregister
收听。
您可以像这样配置路线
index.js
ReactDOM.render(
<BrowserRouter>
<AppContainer>
<Route exact path="/" Component={...} />
<Route exact path="/Home" Component={...} />
</AppContainer>
</BrowserRouter>,
document.getElementById('root')
);
然后在 AppContainer.js中
class App extends Component {
componentWillMount() {
this.unlisten = this.props.history.listen((location, action) => {
console.log("on route change");
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
<div>{this.props.children}</div>
);
}
}
export default withRouter(App);
从历史文档:
您可以使用收听当前位置的更改
history.listen
:history.listen((location, action) => { console.log(`The current URL is
${location.pathname}${location.search}${location.hash}
) console.log(
The last navigation action was ${action}`)
})location对象实现window.location接口的子集,包括:
**location.pathname** - The path of the URL **location.search** - The URL query string **location.hash** - The URL hash fragment
位置也可能具有以下属性:
location.state- 此位置的某些其他状态,不在URL中(在
createBrowserHistory
和中
受支持createMemoryHistory
)
location.key
-代表此位置的唯一字符串(createBrowserHistory
和中支持createMemoryHistory
)该操作是
PUSH, REPLACE, or POP
取决于html" target="_blank">用户如何访问当前URL的操作之一。
当您使用react-router v3时,您可以使用如上所述的history.listen()
fromhistory
包,也可以使用browserHistory.listen()
您可以配置和使用您的路线,例如
import {browserHistory} from 'react-router';
class App extends React.Component {
componentDidMount() {
this.unlisten = browserHistory.listen( location => {
console.log('route changes');
});
}
componentWillUnmount() {
this.unlisten();
}
render() {
return (
<Route path="/" onChange={yourHandler} component={AppContainer}>
<IndexRoute component={StaticContainer} />
<Route path="/a" component={ContainerA} />
<Route path="/b" component={ContainerB} />
</Route>
)
}
}
在标记为副本之前,请仔细阅读此内容,我向您保证,我已经阅读并尝试了每个人在stackoverflow和github上提出的关于此问题的所有建议。 我在我的应用程序中有一条路线,如下所示; route rendering呈现打开用户角色的组件,并基于该角色惰性加载正确的Routes组件,Routes组件呈现主页的开关。简化后,如下所示。 然后是路由组件 因此,问题是每当用户从/导航到/秒等...应用
问题内容: 我目前正在使用React Router v4来嵌套路由。 最接近的示例是React-Router v4文档中的route配置 。 我想将我的应用分为2个不同的部分。 前端和管理区域。 我在想这样的事情: 前端的布局和样式与管理区域不同。因此,在首页中,回家的路线大约应该是子路线。 / home 应该呈现在Frontpage组件中, / admin / home 应该呈现在Backend
问题内容: 我有一个问题要阻止未经授权的用户访问仅授权的路由/组件-例如登录的用户仪表板 我有以下代码: 路由器: 我尝试记录日志以检查是否已输入条件,但是从渲染函数收到错误消息,提示它无法读取null属性。 有没有办法解决我的问题,并且有更好的方法来满足我的要求?只有授权用户才能严格访问特定组件 问题答案: 警告:以下答案使用React的旧上下文API。 如果您使用的是V16.3 +,则以下答案
https://reacttraining.com/react-router/web/example/auth-workflow 我在react-router-dom文档中实现的Private路由 PrivateRoute是从Redux存储获取身份验证状态的已连接组件。 我正在尝试使用redux模拟存储和mount from Ezyme测试连接的组件。 即使状态中的身份验证为真,传递给私人路由的组
问题内容: 我对React还是比较陌生,我正在尝试弄清楚如何使React路由器正常工作。我有一个超级简单的测试应用程序,看起来像这样: 当我运行该应用程序时,home组件加载没有任何麻烦,并且当我单击“ Click Me”链接时,URL更改为localhost / about,但是什么也没有发生。如果单击刷新,则会显示“无法获取/关于”。显然我在做错事,但我一直无法弄清楚是什么。我也在使用Webp
问题内容: 我一直在思考,我对客户端和服务器之间的路由感到困惑。假设我在将请求发送回Web浏览器之前使用ReactJS进行服务器端渲染,并使用react- router作为客户端路由在页面之间切换而不刷新为SPA。 我想到的是: 路线如何解释?例如,从首页()到帖子页面()的请求 路由在服务器端还是客户端去哪里? 它如何知道如何处理? 问题答案: 注意,此答案涵盖了React Router版本0.