[英]Unable to use javascript .replace() method inside the renderRow() function in React Native
好的,事實是,我正在嘗試訪問代碼的renderRow()
函數內的.replace()
方法。
我正在從HubSpot API獲取一些信息,但是某些圖像會保存為http而不是https,這當然會使iOS無法加載該圖像。 與其將Info.plist更改為接受不安全的請求(我不希望Apple有任何借口拒絕我的應用程序),我只想調用.replace()
方法並將http更改為https。
如果我在代碼中的任何地方調用此方法,則效果很好,但是當我在renderRow()
函數中調用該方法時,會出現以下錯誤:
這是一段代碼:
renderRow(post) {
var datePosted = new Date(post.publish_date).toString();
return(
<TouchableHighlight underlayColor='transparent' onPress={() => this.onPostPress(post)}>
<View style={styles.row}>
<Image style={styles.image} source={{uri: post.featured_image.replace(/^http:\/\//i, 'https://')}} /> {/* Here's the error */}
<View style={styles.info}>
<Text style={styles.title}>{post.title}</Text>
<Text style={styles.author}>Por {post.author_name}</Text>
<Text style={styles.date}>{datePosted.substr(4,11)}</Text>
<Text numberOfLines={3} style={styles.description}>{post.meta_description}..</Text>
</View>
</View>
</TouchableHighlight>
);
}
render() {
return(
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow.bind(this)}
style={styles.page}
/>
);
}
請注意,我正確地限制了(this)
。
我能做什么? 提前致謝。
編輯
post.featured_image
已定義 。 當我不嘗試使用replace時,會發生以下情況:
正如我所說,有些圖像在顯示,有些則沒有,因為有些是https (顯示的是一些),有些是http ( 沒有顯示的)。 這就是為什么我需要使用replace()
。
加載API時會出現某種延遲,因此首先undefined
字符串,然后使用API中的信息加載字符串。 我只是添加了一個條件,因此在字符串undefined
情況下它不會嘗試執行任何操作。 現在解決了。 謝謝!!
最終代碼:
if(post.featured_image != undefined) {
var imageHttps = post.featured_image.replace(/^http:\/\//i, 'https://');
}
感謝大家的幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.