【问题标题】:Sending props to stateless components in react native向本机反应中的无状态组件发送道具
【发布时间】:2017-07-10 18:09:29
【问题描述】:

我正在尝试将图像源和标题作为道具发送到 React Native 中的定制无状态组件。 (图片来源和标题)

props.imageSource 行导致错误,感谢任何帮助。这是有问题的代码:

const MainPageButton = (props) => {

    var imageSource = require({props.imageSource});

    return (
        <TouchableOpacity>
          <Image source={imageSource} />
          <Text>{props.title}</Text>
        </TouchableOpacity>
    )
}

【问题讨论】:

    标签: properties react-native components


    【解决方案1】:

    替换

    var imageSource = require({props.imageSource});

    var imageSource = require(props.imageSource);

    props.imageSource 会给你足够你使用的字符串

    【讨论】:

      【解决方案2】:

      您应该改用object destructuring

      const MainPageButton = ({imageSource, title}) => {
      
        var imageSource = require(imageSource);
      
        return (
          <TouchableOpacity>
            <Image source={imageSource} />
            <Text>{title}</Text>
          </TouchableOpacity>
        )
      }
      

      【讨论】:

        猜你喜欢
        • 2020-08-12
        • 2018-03-08
        • 2021-04-21
        • 2019-08-22
        • 1970-01-01
        • 2019-12-26
        • 2018-07-24
        • 2020-02-14
        • 2018-07-01
        相关资源
        最近更新 更多