React:带有大括号的参数与没有大括号的区别

问题描述 投票:0回答:1

考虑这个函数,例如:

  onChange = (event, { newValue }) => {
    this.setState({
      value: newValue,
    });
  };

{ newValue }和使用newValue有什么区别?例子来自here

我正在使用打字稿,正在使用

  onChange = (event, { newValue }: string) => {
    this.setState({
      value: newValue,
    });
  };

任何不同的

  onChange = (event, newValue: string) => {
    this.setState({
      value: newValue,
    });
  };

谢谢你帮我理解!

reactjs typescript reactjs-flux
1个回答
1
投票

如果作为第二个参数将传递一个带有键的对象,例如:

{value:'aaa', newValue: 'bbb', anotherValue: 'ccc'} 

第二个参数将对象属性newValue作为值

换句话说,您可以将整个对象作为第二个参数传递,但只有它的newValue属性将用作第二个参数的值

© www.soinside.com 2019 - 2024. All rights reserved.