简体   繁体   English

使用到达路由器时如何清除 location.state?

[英]How do I clear location.state when using reach-router?

In my React app, I do conditional logic on a route Component's initialization/rendering based on where the user came from.在我的 React 应用程序中,我根据用户的来源对路由组件的初始化/渲染执行条件逻辑。 If he came from Page A, then the Link to the page holds state data that is read from this.props.location.state when the route is rendered.如果他来自页面 A,则指向页面的链接包含 state 数据,这些数据是在渲染路线时从this.props.location.state读取的。 But on refresh, I need the page to “forget” where the user came from (or more specifically, clear location.state ).但是在刷新时,我需要页面“忘记”用户来自哪里(或者更具体地说,清除location.state )。

Unfortunately, Reach Router doesn't use history as a dependency, so it seems solutions like these which advocate for history.update() won't work.不幸的是,Reach Router 不使用history作为依赖项,所以像这样提倡history.update()的解决方案似乎行不通。

To start, the history.update() documentation you were looking at is for react-router, not reach-router (they are very similar but are NOT the same and looking at the wrong docs is only going to cause you headaches).首先,您正在查看的 history.update() 文档适用于 react-router,而不是到达路由器(它们非常相似但并不相同,并且查看错误的文档只会让您头疼)。

The next issue that you might be running into is that you need to figure out what part of the location.state you need to change.您可能遇到的下一个问题是您需要弄清楚 location.state 的哪一部分需要更改。 This would depend on what you set in the state when the user navigates to the page.这取决于您在用户导航到页面时在 state 中设置的内容。 By default, the reach-router state only contains a key, the state that is used to conditionally render is a value set by you.默认情况下,reach-router state 只包含一个键,用于条件渲染的 state 是您设置的值。 So what you need to remove is the location.state.value (value being whatever you set) by setting the value to null or false which should stop the conditional rendering.因此,您需要删除的是 location.state.value (值是您设置的任何值),方法是将值设置为 null 或 false,这应该会停止条件渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM