简体   繁体   English

为什么对对象 setState 使用 ES6 计算属性语法?

[英]Why use ES6 computed property syntax for object setState?

In an example of the React docs page Forms , ES6 computed property syntax is used in a method to set the state of the name property.在 React 文档页面Forms的示例中,ES6 计算属性语法在方法中用于设置name属性的状态。

handleInputChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

Based on my reading of how the computed property works, it seems like the reason it's used is so target.name can be changed -- is that so?根据我对计算属性如何工作的阅读,似乎使用它的原因是target.name可以更改——是这样吗? If that's the case it seems like it would just be easier to change it there in setState rather than changing the value of the name variable.如果是这种情况,那么在setState中更改它似乎比更改name变量的值更容易。

I'm new to React and struggling to understand how the computed property syntax is applied in this example.我是 React 的新手,正在努力理解如何在这个例子中应用计算属性语法。 Any help would be greatly appreciated.任何帮助将不胜感激。

Why use ES6 computed property syntax for object setState?为什么对对象 setState 使用 ES6 计算属性语法?

The computed property syntax allows you to set the key of an object dynamically .计算属性语法允许您动态设置对象的键

In the case of setState , it allows you to handle different properties of the state with a single setState , and so to reuse the same event handler function on different inputs.setState的情况下,它允许您使用单个setState处理状态的不同属性,因此可以在不同的输入上重用相同的事件处理函数。

So instead of:所以不是:

class Reservation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isGoing: true,
      numberOfGuests: 2
    };

    this.handleIsGoingChange = this.handleIsGoingChange.bind(this);
    this.handleNumberOfGuestsChange = this.handleNumberOfGuestsChange.bind(this);
  }

  // a first handler, for isGoing
  handleIsGoingChange(event) {
    const target = event.target;
    const value = target.checked;

    this.setState({
      isGoing: value
    });
  }

  // a second handler, for numberOfGuests
  handleNumberOfGuestsChange(event) {
    const target = event.target;
    const value = target.value;

    this.setState({
      numberOfGuests: value
    });
  }

  render() {
    return (
      <form>
        <label>
          Is going:
          <input
            name="isGoing"
            type="checkbox"
            checked={this.state.isGoing}
            onChange={this.handleIsGoingChange} />
        </label>
        <br />
        <label>
          Number of guests:
          <input
            name="numberOfGuests"
            type="number"
            value={this.state.numberOfGuests}
            onChange={this.handleNumberOfGuestsChange} />
        </label>
      </form>
    );
  }
}

You can shorten it like this:你可以这样缩短它:

class Reservation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isGoing: true,
      numberOfGuests: 2
    };

    this.handleInputChange = this.handleInputChange.bind(this);
  }


  // a single handler, for isGoing and numberOfGuests
  handleInputChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

  render() {
    return (
      <form>
        <label>
          Is going:
          <input
            name="isGoing"
            type="checkbox"
            checked={this.state.isGoing}
            onChange={this.handleInputChange} />
        </label>
        <br />
        <label>
          Number of guests:
          <input
            name="numberOfGuests"
            type="number"
            value={this.state.numberOfGuests}
            onChange={this.handleInputChange} />
        </label>
      </form>
    );
  }
}

When you wrap a key with array square brackets it will get the variable name as a key.当您用数组方括号包裹一个键时,它将获得变量名作为键。

If you doesn't the key will be string.如果你不这样做,密钥将是字符串。 So...所以...

let name = 'id';
let obj = { //let obj = {
  [name]:1  // id: 1
};          //};

Cause you don't want to set the "name" property, but the property which name is stored in name.因为您不想设置“名称”属性,而是名称存储在名称中的属性。

  var name = "test";

  // these are all equal:
  this.setState({ [name]: 1 })
  this.setState({ ["test"]: 1 })
  this.setState({ test: 1 })

If you don't use the computed property syntax, your function would always set the name property instead of the computed value from event.target.name , which is what you want.如果您不使用计算属性语法,您的函数将始终设置name属性而不是event.target.name的计算值,这正是您想要的。 Your state would always look like this:您的状态将始终如下所示:

console.log(this.state);
// -> { name: 'some value' }

Maybe by writing without the ES6 syntax you will understand more what's going on.也许通过不使用 ES6 语法编写你会更了解发生了什么。

The same code would be as below (you could run it in the snippet and see it)相同的代码如下(您可以在代码段中运行它并查看它)

One thing I would tell though is use let instead of const simply because variables created with const constantly point or bind to the same value as long as they "live".我要说的一件事是使用let而不是const ,因为用const创建的变量只要“存在”,就会不断指向或绑定到相同的值。 So, using const here may not let you check and/or uncheck the box, or let you increase/decrease the number.因此,在这里使用const可能不会让您选中和/或取消选中该框,或者让您增加/减少数字。

I hope it helps you understand more.我希望它能帮助你了解更多。

Thanks谢谢

 class Reservation extends React.Component { constructor(props) { super(props); this.state = { isGoing: true, numberOfGuests: 2 }; this.handleInputChange = this.handleInputChange.bind(this); } handleInputChange(event) { let value; if(event.target.type==='checkbox'){ value = event.target.checked } else { value = event.target.value } this.setState({ [event.target.name]: value }); } render() { return ( <form> <label> Is going: <input name="isGoing" type="checkbox" checked={this.state.isGoing} onChange={this.handleInputChange} /> </label> <br /> <label> Number of guests: <input name="numberOfGuests" type="number" value={this.state.numberOfGuests} onChange={this.handleInputChange} /> </label> </form> ); } } ReactDOM.render( <Reservation />, document.getElementById('root') );
 <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script> <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <div id="root"></div>

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

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