【发布时间】:2018-05-02 08:50:30
【问题描述】:
我正在寻找解决方案 - 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;
我试图通过道具将状态推入位置和历史。 但是每当我从下一页按“浏览器按钮返回”时,它总是会重置状态。 谁能告诉我哪里做错了?
【问题讨论】:
-
不确定您要达到的目标。是否要根据 url 存储计数值?所以例如在查询参数中?
标签: javascript reactjs react-router