【发布时间】:2018-05-16 01:44:32
【问题描述】:
'react-router-dom'有刷新功能here,但是我不知道怎么调用这个方法,他们的well formatted document也懒得解释了。
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>
)
}
}
【问题讨论】:
-
“刷新”一条路线听起来像 xy 问题meta.stackexchange.com/questions/66377/what-is-the-xy-problem 你到底想达到什么目的?
-
您可能应该为此创建一个 jsfiddle。当它观察到的数据发生变化时,组件会重新渲染。那是不是没有发生。
标签: reactjs react-router react-router-v4