简体   繁体   English

使用 setState 设置 react 页面的所有状态

[英]Using setState to set all state of react page

I have a page where I'm gonna take the whole state of the page and set it with some localstorage so I need to use SetState but not using some property like sane people do我有一个页面,我将在其中获取页面的整个状态并使用一些本地存储对其进行设置,因此我需要使用 SetState 但不能像理智的人那样使用某些属性

Sane people理智的人

this.setState({ 
  stateProperty:someValue
})

Me (what I want to achieve)我(我想要达到的目标)

this.setState({
  this.state: {someValues}
})

No, you don't.不,你没有。 Sorry, you can't.对不起,你不能。 You've accepted yourself that is sane people do like, but why are you trying to go insane code?你已经接受了自己是理智的人喜欢的,但你为什么要尝试疯狂的代码?

It's better to maintain for localStorage data with specified state:最好维护具有指定状态的 localStorage 数据:

state = {
  storeData: {} // or, [], or whatever your stored data type
}
componentDidMount() {
  const storeData = JSON.parse(localStorage.getItem('store-data'))
  this.setState({
     storeData
  })
}

Some people might suggest you to initialize localStorage data directly in the constructor.有些人可能会建议您直接在构造函数中初始化 localStorage 数据。 But I'm against it because I don't like to cause some side effect to the component.但我反对它,因为我不喜欢对组件造成一些副作用。

PS: There' no side effect actually using localStorage data in the constructor. PS:实际上在构造函数中使用localStorage数据没有副作用。 I have just suggested to use componentDidMount hook because you might need to call some api which will call to load the localStorage data and that time you might have some side effect.我刚刚建议使用componentDidMount钩子,因为您可能需要调用一些 api 来调用加载localStorage数据,那时您可能会产生一些副作用。 To ignore this, use componentDidMount hook because it's an asynchronous function which runs in background.要忽略这一点,请使用componentDidMount钩子,因为它是一个在后台运行的异步函数。

For set/get state from localStorage use this:要从 localStorage 设置/获取状态,请使用以下命令:

localStorage.setItem('store-state',JSON.stringify(this.state));
const storeState = JSON.parse(localStorage.getItem('store-state'));
this.setState(storeState);

With this code you able to store/reload your state from localStorage.使用此代码,您可以从 localStorage 存储/重新加载您的状态。 notice, in setState no need to create any objects({}).注意,在 setState 中不需要创建任何对象({})。

Maybe you could do something like this:也许你可以这样做:

const updateState = window.localStorage.getItem('whatever');
let newState = Object.assign({}, this.state);
newState = updateState;
this.setState({newState});

Hope it works.希望它有效。

Looks like you can actually call a this.state={something} on constructor看起来您实际上可以在构造函数上调用 this.state={something}

This did the trick这成功了

constructor(props) {
        super(props);

          if ( typeof store.get([store.get("auth").ID]) === "undefined") {
              store.set([store.get("auth").ID], { localStorageData: {} });
          }

          this.state = {
              ...bla bla bla
          }

          this.state=Object.assign({}, this.state, this.state.localstore ) 
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM