我正在寻找解决方案 - react-router v4 不保持组件的先前状态。例如,这是一个简单的计数器:
import React, { Component } from "react";
class Schedule extends Component {
constructor(props) {
super(props);
this.state = {
counter: 0
};
this.holdState = this.holdState.bind(this);
}
holdState() {
this.props.location.state = this.state.counter;
const state = this.state;
this.setState({
counter: state.counter + 1
});
}
render() {
return (
<div>
<ul>
<li>6/5 @ Evergreens</li>
<li>6/8 vs Kickers</li>
<li>6/14 @ United</li>
</ul>
<div>
<button onClick={() => this.holdState()}>Click</button>
<span>{this.state.counter}</span>
</div>
</div>
);
}
}
export default Schedule;
我试图通过props将状态推入位置和历史。但是每当我从下一页按下“浏览器按钮返回”时,它总是会重置状态。有人能告诉我我在哪里做错了吗?