【问题标题】:React: Difference between argument with braces vs without bracesReact:带大括号的参数与不带大括号的参数之间的区别
【发布时间】:2019-07-01 02:41:47
【问题描述】:

以这个函数为例:

  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】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-27
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-11
    • 1970-01-01
    • 2023-03-19
    相关资源
    最近更新 更多