javascript - 图像组件上的 React-native 失败 propType

标签 javascript xcode reactjs react-native

我刚刚开始使用 React-native,并且对用于创建 Web 应用程序的 React 有相当不错的掌握...我在这里遇到了一个令人困惑的问题,而在使用 React 进行 Web 工作时从未出现过。

基本上,我正在渲染一个组件,其图像是通过映射其父组件中的对象数组来动态生成的。

export default class ImageComponent extends React.Component {
  render() {
    return (
        <Image source={this.props.source}/>
    );
  }
};

及其父组件:

export default class MainComponent extends React.Component {
  constructor(props) {
    super(props);
   this.state = {
      images:[
        { src: './src/images/1.png'},
        { src: '/src/images/2.png'},
        { src: './src/images/3.png'}
      ]
    }
   }

  render() {
    let images = this.state.images.map((image) => {
      return(<ImageComponent source={image.src}  />);
    })

    return (
      <View>
        {images}
      </View>
    );
  }
};

在设备模拟器中,我收到以下错误:

“警告:失败的 propType:提供给“Image”的 Prop “source”无效,请检查“BasicComponent”的渲染方法”

有人知道这里会发生什么吗?

最佳答案

您应该需要本地 Assets 或使用带有 uri 键的对象。

所以要么在 MainComponent 中:

this.state = {
  images:[
    require('./src/images/1.png'),
    require('./src/images/2.png'),
    require('./src/images/3.png')
  ]
}

或在BasicComponent中:

return (
  <Image 
    source={{uri: this.props.source}}
  />
);

关于javascript - 图像组件上的 React-native 失败 propType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36460827/

相关文章:

javascript - 关于图像映射的一般问题

objective-c - 如何在 Xcode 中为所有相同的方法调用添加断点?

xcode - 使用 Swift 的预期声明错误

javascript - 单击 react 中的提交按钮迁移到新组件

javascript - Moment.js 错误地创建了一个新日期

javascript - 分配存储在 jQuery.data() 中的对象的各个键

javascript - 如何使用angular 6和bootstrap 3.3.7创建带有复选框列表的可折叠/可展开/树结构

swift - 即使网点看起来正确,如何解决错误 SIGABRT?

javascript - react 对象表达?

javascript - 在循环中获取数据