React native 要求正好有一个字符串文字参数的调用

React native 要求正好有一个字符串文字参数的调用,react-native,React Native,我想根据传递的道具渲染图像。第一个注释掉的文件路径就是源文件路径。我试过几种不同的方法,但它总是给我同样的错误。react文档并没有真正涵盖在30多个不同场景的情况下应该做什么。我可以在这个组件中添加一个状态,让它包含所有的文件变体,但这似乎需要做更多的工作。 如果有大量可能的图像,可以创建包含所有可能图像的对象,然后在图像源中传递图标名称 let icons = { 'sample1': require('./weatherimg/sample1.png') ,'sample2':

我想根据传递的道具渲染图像。第一个注释掉的文件路径就是源文件路径。我试过几种不同的方法,但它总是给我同样的错误。react文档并没有真正涵盖在30多个不同场景的情况下应该做什么。我可以在这个组件中添加一个状态,让它包含所有的文件变体,但这似乎需要做更多的工作。


如果有大量可能的图像,可以创建包含所有可能图像的对象,然后在图像源中传递图标名称

let icons = {
  'sample1': require('./weatherimg/sample1.png')
  ,'sample2': require('./weatherimg/sample2.png')
  ,'sample3': require('./weatherimg/sample3.png')
}

render(){
  return(
    <Image source={icons[this.props.icon.icon]}/>
  )
}

说明您不需要动态文件,并提供了一个示例,说明在可能的选项很少的情况下如何处理动态文件。

如果您有大量可能的图像列表,则可以创建一个包含所有可能图像的对象,然后在图像源中传递图标名称

let icons = {
  'sample1': require('./weatherimg/sample1.png')
  ,'sample2': require('./weatherimg/sample2.png')
  ,'sample3': require('./weatherimg/sample3.png')
}

render(){
  return(
    <Image source={icons[this.props.icon.icon]}/>
  )
}

说明您不需要动态文件,并提供了一个示例,说明在选项很少的情况下如何处理动态文件。

let filePath2=this.props.icon?requireflepath:requireflepath;呵呵?无论条件结果如何,都使用相同的结果?我的组件从我用来生成文件路径的道具获取信息。React native中是否不允许使用温和的文字?即使我在传递文件路径的较高组件中定义了文件路径,它也不会接受它。在这一点上,它不应该只是一个字符串吗?为什么在react-native中温和的文本是一个问题。我说的是让filePath2=this.props.icon?requireflepath:requireflepath;是无用的,不妨简化它,让filePath2=requireflepath;它给出了相同的错误。是否让filePath2=this.props.icon?requireflepath:requireflepath;呵呵?无论条件结果如何,都使用相同的结果?我的组件从我用来生成文件路径的道具获取信息。React native中是否不允许使用温和的文字?即使我在传递文件路径的较高组件中定义了文件路径,它也不会接受它。在这一点上,它不应该只是一个字符串吗?为什么在react-native中温和的文本是一个问题。我说的是让filePath2=this.props.icon?requireflepath:requireflepath;是无用的,不妨简化它,让filePath2=requireflepath;它给出了同样的错误。
let icons = {
  default: require('./weatherimg/default.png')
  ,'sample1': require('./weatherimg/sample1.png')
  ,'sample2': require('./weatherimg/sample2.png')
  ,'sample3': require('./weatherimg/sample3.png')
}

validateIcons(icon){
  return (icons[icon]) ? icons[icon] : icons['default'];
}

render(){
  return(
    <Image source={this.validateIcons(this.props.icon.icon)}/>
  )
}