【问题标题】:Destructure property whose name is a reserverd keyword [duplicate]名称为保留关键字的解构属性[重复]
【发布时间】:2020-03-31 20:15:55
【问题描述】:

在使用材质 ui 时,我意识到他们在转场组件中有一个名为 in 的道具,但在尝试破坏道具时,我不能,因为 in 是保留关键字。

const MyFade = ({ children, in, ...otherProps }) => { // this gives me an error 
  return (
    <div {...otherProps}>
      <Fade in={in}>{children}</Fade>
    </div>
  );
};

我该怎么做?我需要破坏in 并让otherPropsdiv 中传播。

【问题讨论】:

    标签: javascript reactjs ecmascript-6 destructuring


    【解决方案1】:

    只需在解构中分配一个新的、非保留的名称。

    const o = {
       in: 'foo',
       out: 'boo',
    };
    
    const { in: inProp } = o;
          // ^^^^ assign new name
    
    console.log(inProp);

    【讨论】:

      猜你喜欢
      • 2017-07-06
      • 2021-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-15
      • 2019-06-17
      • 1970-01-01
      相关资源
      最近更新 更多