道具破坏反应

时间:2020-06-18 01:47:08

标签: javascript reactjs

我的道具道具看起来像

adder

我抓住这样的道具

BinaryPoint

我试图破坏道具对象 并使用所有这些横幅,内容,徽标,标题

但编译器抱怨横幅为未定义。 我在破坏结构方面做错了吗?

2 个答案:

答案 0 :(得分:1)

在解构如下对象时,仅解压缩logotitlecontent

  const { 
    banner: { logo, title },
    content
  } = props; 

这就是为什么在您的问题someFunction中,您可以这样做:

function someFunction() {
  if (logo) {
  ...
    if (title) {
    ...
    }
  }
}

如果您还想使用变量banner,则必须执行以下操作:

const { banner, content } = props;
const { logo, title } = banner;

详细了解Destructuring assignment

答案 1 :(得分:0)

简单,您实际上并没有从道具中获取banner变量,而是使用了标题和徽标。

您可以这样做:

const MyComponent = (props) => {
  const { 
    banner,
    content
  } = props; 
  ... 
  function someFunction(){
    if( banner.logo) {
      if(banner.title)
    }
   ... 
  }
  ... 
}