简体   繁体   English

如何防止位置对象在重新加载 reactjs 时重置

[英]how to prevent location object to reset on reload reactjs

I am using Link to re-direct call another component.我正在使用 Link 来重新直接调用另一个组件。 Something like this像这样的东西

<Link to={{ pathname: "/app/" + app.appId, appDetail: { app: app } }}>>

On calling path /app/:appId It calls AppDetails Component.在调用路径 /app/:appId 它调用 AppDetails 组件。 And I'm getting appDetail object in location object.我在 location 对象中获取 appDetail 对象。

But on reloading the route /app/:appId this appDetail in location object is becoming undefined.但是在重新加载路线 /app/:appId 时,位置对象中的这个 appDetail 变得未定义。 So How can I prevent this so that on reloading /app/:appId this appDetail from the location object should not get undefined那么如何防止这种情况发生,以便在重新加载 /app/:appId 时,来自 location 对象的 appDetail 不应未定义

there are many ways you can control this behavior here is one: try putting location object in if condition like:有很多方法可以控制这种行为,这里是一种:尝试将位置对象放在 if 条件中,例如:

if(!location) {
  return <>Loading...</>
}

this above condition will hold execution till condition meets上述条件将保持执行,直到条件满足

There is state property to pass data to other component.state属性可以将数据传递给其他组件。 So you can do something like this:所以你可以做这样的事情:

<Link to={{
  pathname: "/app/" + app.appId,
  state: { appDetail: { app: app }}
}}>

And get appDetail in other component:并在其他组件中获取appDetail

const { appDetail } = this.props.location.state;

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

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