React - 刷新保存计数器

React - Refreshing Save Counter

我有一个组件,它运行良好

好的,在构造函数中我有:

  constructor(props) {
    super(props);
    this.state = {
        count: 0
    }

我还有功能:

onClick(e) {
    this.setState({
        count: this.state.count + 1
    });
}

如何让计数不是每次0,而是刷新后更新?

下面是一个增加计数器和重置计数器的简单示例。 如果您希望此值在页面重新加载后仍然存在,则无法将值保持在状态中。不确定您是否只在寻找这个

class TestJs extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            count: 0
        }
        this.onClick = this.onClick.bind(this);
        this.resetCounter = this.resetCounter.bind(this);
    }

    onClick(e) {
        this.setState({
            count: this.state.count + 1
        });
    }

    resetCounter(){
        this.setState({count : 0});
    }

    render() {
        return (
            <div>
                Counter value is {this.state.count}
                <br/>
                <button onClick={this.onClick}> Increase counter</button>
                <br/>
                <button onClick={this.resetCounter}> Reset counter</button>
            </div>
        );
    }
}

export default TestJs