‘react-router-dom’ 这里 有刷新功能,但我不知道如何调用这个方法,而且他们 格式良好的文档 也懒得解释这个。
window.location.reload() 对我不起作用,因为它也会清除应用商店。我更改了一些数据,然后需要使用更新的数据重新加载路线。
我也读过这个: https ://github.com/ReactTraining/react-router/issues/1982 https://github.com/ReactTraining/react-router/issues/2243
这个线程正是在讨论我的问题: https ://github.com/ReactTraining/react-router/issues/4056
而且仍然不知道该怎么做。这个基本功能应该不需要太多的努力,但是在花费了巨大的努力之后,我无法重新加载当前的路线。
这是我的代码:
@inject('store') @observer
export default class PasswordInput extends Component {
constructor (props) {
super(props)
this.setPwd = this.setPwd.bind(this)
this.submit = this.submit.bind(this)
}
componentWillMount() {
this.case = this.props.store.case
this.setState({refresh: false})
}
setPwd(e) {
// console.log(e)
this.case.pwd = e.target.value
}
submit() {
console.log(this.props.caseId)
this.setState({refresh: true})
}
render() {
return (
<Container>
<div>{this.state.refresh}</div>
{ (this.state.refresh) && <Redirect refresh={true} to={'/cases/' + this.props.caseId}></Redirect>}
<br />
<Grid columns="three">
<Grid.Row>
<Grid.Column key={2}>
<Form>
<label>Enter Password</label>
<input placeholder="empty" type="text" onChange={this.setPwd} value={this.case.pwd}></input>
<Button name="Save" color="green" size="mini"
style={{marginTop:'1em'}}
onClick={this.submit}>
Submit
</Button>
</Form>
</Grid.Column>
</Grid.Row>
</Grid>
</Container>
)
}
}
原文由 Nicolas S.Xu 发布,翻译遵循 CC BY-SA 4.0 许可协议
我通过将一条新路线推入历史记录,然后用当前路线(或您要刷新的路线)替换该路线来解决这个问题。这将触发 react-router 在不刷新整个页面的情况下“重新加载”路由。
React router 的工作原理是使用您的浏览器历史记录进行导航,而无需重新加载整个页面。如果你强制路由进入历史反应路由器将检测到这一点并重新加载路由。替换空路由很重要,这样你的后退按钮在你按下后不会把你带到空路由。
根据 react-router 的说法,react router 库似乎不支持此功能,而且可能永远不会支持,因此您必须以一种 hacky 的方式强制刷新。