清除状态es6 React

Guy*_*Guy 53 javascript reactjs

我试图清除组件state但无法找到es6语法的参考.我用的是:

this.replaceState(this.getInitialState());

但这不适用于es6类语法.

我怎样才能达到同样的效果?

Dav*_*lsh 83

据我所知,React组件不保留初始状态的副本,因此您只需自己完成.

const initialState = {
    /* etc */
};

class MyComponent extends Component {
    constructor(props) {
        super(props)
        this.state = initialState;
    }
    reset() {
        this.setState(initialState);
    }
    /* etc */
}
Run Code Online (Sandbox Code Playgroud)

请注意,该行this.state = initialState;要求您永远不要改变您的状态,否则您将污染initialState并使重置变得不可能.如果你无法避免突变,那么你需要initialState在构造函数中创建一个副本.(或initialState按功能制作功能getInitialState().)

最后,我建议你使用setState()而不是replaceState().

  • 这不能正常工作,因为如果你改变`state`然后它也会改变`initialState`,因为它是通过引用分配的.因此,当您调用reset时,它实际上不会重置为`initialState`.没有什么变化,你必须将`initialState`深度复制到`state` (10认同)
  • 请注意`setState`合并状态.如果组件将状态添加到不在"initialState"中的状态,则不会清除这些字段. (6认同)
  • 如果您要将状态添加到未在初始状态中定义的状态,那么您做错了. (6认同)
  • 事实上,当使用ES6类时,甚至不能使用`replaceState()`.自从我发布这个答案后,我才意识到这一点. (3认同)
  • 这是我使用的方法。我确实想评论一下,在这种情况下,`replaceState`可以很好地使用,因为它可以确保状态对象的_entirety_返回到一组已知值。 (2认同)

Rap*_*toX 44

问题

接受的答案:

const initialState = {
    /* etc */
};

class MyComponent extends Component {
    constructor(props) {
        super(props)
        this.state = initialState;
    }
    reset() {
        this.setState(initialState);
    }
    /* etc */
}
Run Code Online (Sandbox Code Playgroud)

不幸的是不正确.

initialState作为参考传递this.state,所以每当你改变state你也改变initialState(const在这里并不重要).结果是你永远不能回去initialState.

你必须深拷贝 initialStatestate,那么它会工作.无论是写一个深拷贝功能都自己或者使用一些现有的模块像这样.

  • 在引擎盖下,React正在做一个`Object.assign({},nextState,partialState);`.因此,没有必要对initialState进行深层复制,因为this.state始终是setState之后的新对象.好吧,Object.assign只克隆一个级别......但是使用setState方法保存的所有状态都应该不改变状态,传递带有更改的新副本. (8认同)
  • 如果这个答案是正确的,那么当我将状态设置为初始状态然后尝试再次将其重置时,我将遇到问题。但这根本没有发生。对这个答案投票的用户并不能真正理解正在发生的事情。我现在非常沮丧,以至于很多人被这种可疑的理解所欺骗,尤其是因为它是如此容易测试。 (2认同)

Eru*_*aca 26

这是作为函数实现的解决方案:

Class MyComponent extends React.Component {
   constructor(props) {
      super(props);
      this.state = this.getInitialState();
   }

   getInitialState = () => {
     const initialState = {
        /* state props */
     };
     return initialState;
 }

  resetState = () => {
     this.setState(this.getInitialState());
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 使用此选项,我不需要进行深拷贝。 (5认同)

Cha*_*her 14

涉及this.state直接设置的解决方案在React 16中对我不起作用,所以这是我重置每个键所做的:

  const initialState = { example: 'example' }
  ...
  constructor() {
      super()
      this.state = initialState
  }
  ...
  reset() {
    const keys = Object.keys(this.state)
    const stateReset = keys.reduce((acc, v) => ({ ...acc, [v]: undefined }), {})
    this.setState({ ...stateReset, ...initialState })
  }
Run Code Online (Sandbox Code Playgroud)


Bal*_*d02 5

首先,在使用componentWillMount()组件生命周期中的函数时,您需要存储初始状态:

componentWillMount() {
    this.initialState = this.state
}
Run Code Online (Sandbox Code Playgroud)

这将存储您的初始状态,并可用于在需要时通过调用重置状态

this.setState(this.initialState)
Run Code Online (Sandbox Code Playgroud)


uke*_*tar 5

const initialState = {
    a: '',
    b: '',
    c: ''
};

class ExampleComponent extends Component {
    state = { ...initialState } // use spread operator to avoid mutation
    handleReset = this.handleReset.bind(this);

    handleReset() {
         this.setState(initialState);
    }
 }
Run Code Online (Sandbox Code Playgroud)

请记住,为了能够重置状态,重要的是不要更改initialState。

state = {...initialState} // GOOD 
// => state points to a new obj in memory which has the values of initialState

state = initialState // BAD 
// => they point to the same obj in memory
Run Code Online (Sandbox Code Playgroud)

最方便的方法是使用ES6 Spread Operator。但您也可以改用Object.assign。他们都将实现相同的目标。

state = Object.assign({}, initialState); // GOOD
state = {...initialState}; // GOOD
Run Code Online (Sandbox Code Playgroud)