Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 图像源uri内的if语句-react native_Javascript_Reactjs_React Native_Jsx - Fatal编程技术网

Javascript 图像源uri内的if语句-react native

Javascript 图像源uri内的if语句-react native,javascript,reactjs,react-native,jsx,Javascript,Reactjs,React Native,Jsx,有一个api有时会为图像URL发送空字段,而react native tell me source.uri不应该为空,但我更担心,因为它破坏了我的网格布局。我想用一个相同大小的占位符来解决这个问题。 这就是我到目前为止所尝试的 <View> <Image source={{ !this.item.thumbnail.length ? uri: item.thumbnail : {require('/assets/images/placeholder.jpg')

有一个api有时会为图像URL发送空字段,而react native tell me source.uri不应该为空,但我更担心,因为它破坏了我的网格布局。我想用一个相同大小的占位符来解决这个问题。 这就是我到目前为止所尝试的

<View>
    <Image source={{ !this.item.thumbnail.length ? uri: 
    item.thumbnail : {require('/assets/images/placeholder.jpg')} }} />
</View>

请任何人帮忙。

您的尝试非常接近,但语法不正确。您只需要确保在三元函数的一个分支中,您返回一个带有uri字段的对象,而在另一个分支中,您直接返回require调用,而不是像在示例中那样包装它

例如:


注:我扭转了你的状况,这对我来说似乎是倒退。如果缩略图长度不为零,则需要使用缩略图,否则需要使用占位符,因此我删除了否定项。

使用第三方库解决我的问题:

import ImageLoad from 'react-native-image-placeholder';

<View>
  <ImageLoad style={styles.pix} source={{uri: item.thumbnail}}/>
</View>  

这个答案并不完全属于你的问题范围,但它可能会有所帮助。 为什么不将源定义为返回之外的变量

比如:

let newSauce = !this.item.thumbnail.length ? uri: 
    item.thumbnail : {require('/assets/images/placeholder.jpg')}
return(
    <View>
         <Image source={{ newSauce }} />
    </View>
)

placeholder.jog或placeholder.jpg?uri:item.thumbnail应该是什么?@adam使用flatlist和renderitem@dacre-denny Typo这里肯定有语法错误,而且我也没有尝试过这个.item.thumbnail.length,所以不确定它在平面列表中工作,不确定item.thumbnail会通过,谢谢
let newSauce = !this.item.thumbnail.length ? uri: 
    item.thumbnail : {require('/assets/images/placeholder.jpg')}
return(
    <View>
         <Image source={{ newSauce }} />
    </View>
)