首页 > 解决方案 > 在 React Native 中将数组作为道具传递

问题描述

我一直在阅读很多线程,但找不到解决问题的方法。

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

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

当我控制台记录页脚中的参数时,我得到:

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

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

console.log(buttonsActive.firstButton);

我收到一个未定义的错误。在不使用 .map() 并询问 if (var == firstButton).... 的情况下访问这两个值的方法是什么?

谢谢

标签: reactjsreact-native

解决方案


我终于明白了它是如何工作的,所以可以这样发送参数:

<Footer firstButton secondButton={false} />

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

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

我希望它有所帮助。


推荐阅读