Javascript 如何保存切换状态和don';刷新页面后不会丢失

Javascript 如何保存切换状态和don';刷新页面后不会丢失,javascript,reactjs,redux,react-redux,Javascript,Reactjs,Redux,React Redux,你好,我的项目主要由react和redux完成, 我正在构建应用程序在这个应用程序有切换时,它的数据同步到日历上,我希望它保持它的切换,即使刷新页面。 这里有一些代码 constructor(props){ super(props); this.state ={ value: 1, toggled: undefined }; this.handleToggle = this.handleToggle.bind(this); }

你好,我的项目主要由react和redux完成, 我正在构建应用程序在这个应用程序有切换时,它的数据同步到日历上,我希望它保持它的切换,即使刷新页面。 这里有一些代码

constructor(props){
    super(props);

    this.state ={
        value: 1,
        toggled: undefined
    };
    this.handleToggle = this.handleToggle.bind(this);
} 

handleToggle = (event,toggled,index) => {
    this.setState({toggled});

    if (toggled == true){
         ///sync the Calendar code////
    }else{
        /// un sync ////
    }
}
回来后在这里

  <Toggle label={translate('sync')}
          onToggle={this.handleToggle}
          toggled={this.state.toggled}
  />


除此之外是否还有其他方法保存状态标记?

在卸载时将状态保存在localStorage中,并在初始装载时重新填充

constructor(props){
    super(props);
    const local = localStorage.getItem('state');
    if(local) {
       this.state =JSON.parse(local)
    } else {
          this.state ={
               value: 1,
               toggled: undefined
          };
          this.handleToggle = this.handleToggle.bind(this);
    }
}

handleToggle = (event,toggled,index) => {
    this.setState({toggled});
    if (toggled == true){
         ///sync the Calendar code////
    }else{
       /// un sync ////
}

componentWillUnmount() {
   localStorage.setItem('state', JSON.stringify(this.state));
}

这是有效的,但当我切换回某个时间需要一次以上的时间才能返回,为什么?这与persistedReducer有关吗?persistedReducer仅在redux存储中更改,可能是因为状态设置不正确