gpt4 book ai didi

reactjs - React Router : Replace location state then goBack, 位置状态未定义

转载 作者:行者123 更新时间:2023-12-04 10:42:42 25 4
gpt4 key购买 nike

this.props.history.replace({
state: {
from: 'dashboard'
}
});

this.props.history.goBack();

更新this.props.history.goBack()后,this.props.location.state的值为undefined this.props.location.state。但是

this.props.history.push({
pathname: '/',
state: {
from: 'dashboard'
}
});

有效并保留 this.props.location.state 的值。

知道如何更新 this.props.location.state 然后使用 this.props.history.goBack 吗?

最佳答案

根据官方文档:

  • push(path, [state]) - (function) 将新条目推送到历史堆栈
  • replace(path, [state]) - (function) 替换历史堆栈中的当前条目
  • go(n) - (function) 将历史堆栈中的指针移动 n 个条目
  • goBack() - (function) 相当于 go(-1)
  • goForward() - (function) 相当于 go(1)

如果您想使用 goBack(),最好使用 this.props.history.push({})

为了更好地了解您可以访问react-router-dom , history

关于reactjs - React Router : Replace location state then goBack, 位置状态未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59851872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com