在React Native中将数组作为道具传递

时间:2019-02-28 12:48:04

标签: reactjs react-native

我已经阅读了很多线程,但是找不到解决我问题的方法。

我通过以下方式将参数传递给另一个React Native组件:

<Footer buttonsActive={{ firstButton: 'true', secondButton: 'false' }} />

当我在页脚中登录参数时,我得到:

Object {
  "buttonsActive": Object {
    "firstButton": "true",
    "secondButton": "false",
  },
}

看起来不错,但是当我尝试通过以下方式控制台记录元素时:

console.log(buttonsActive.firstButton);

我收到未定义的错误。通过使用.map()并询问是否(var == firstButton)来访问这两个值而不进行迭代的方式是什么?

谢谢

2 个答案:

答案 0 :(得分:0)

可能是您的上下文中找不到的buttonActive对象。它应该是页脚内部的道具。 也许尝试在页脚中使用console.log(this.props.buttonsActive.firstButton);

答案 1 :(得分:0)

我终于了解了它是如何工作的,因此可以通过这种方式发送参数:

<Footer firstButton secondButton={false} />

然后以这种方式在页脚组件中访问它们:

const Footer = (buttonStatus) => {
    const firstButtonColor = (buttonStatus.firstButton ? 'black' : 'grey');
    const SecondButtonColor = (buttonStatus.secondButton ? 'black' : 'grey');

希望对您有帮助。