解决 React Native 中“source.uri 不应为空字符串”的问题

Hug*_*ugo 7 javascript react-native

我无法解决

source.uri 不应为空字符串

在 React Native 中。

我不明白这个错误来自哪里。我的组件中有 3 个Flatlist,它使用来自父级的道具作为 URI呈现组件,并且这些都不是空的。

这是uri部分:

<Image style={styles.imageStyle} source={{uri: this.props.url }} />
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

小智 15

这是另一个解决方案:

<Image style={styles.imageStyle} source={this.props.url ? {uri: this.props.url } : null} />
Run Code Online (Sandbox Code Playgroud)


Ben*_*zir 5

很可能向 uri 传递了一个空字符串。如果您像我一样故意将 uri 设置为空字符串,以便不显示此数据的任何图像,请使用 undefined 代替或像这样使用它:

<Image style={styles.imageStyle} source={{uri: this.props.url !=="" ? this.props.url : undefined }} />
Run Code Online (Sandbox Code Playgroud)


Hug*_*ugo 2

好吧,我设法通过添加默认的INITIAL_STATE变量来解决这个问题,该变量在执行操作之前设置我的应用程序的状态和默认值。

因此添加下面的网址即可解决!但可以是空值以外的任何 url。

profile: { user: { phone: '', email: '', first_name: '', last_name: '', photo: 'http://www.tiptoncommunications.com/components/com_easyblog/themes/wireframe/images/placeholder-image.png', description: '' }, membership: { active: false } }
Run Code Online (Sandbox Code Playgroud)