亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法在本機反應中使用來自 FlatLists renderItem 的項目值

無法在本機反應中使用來自 FlatLists renderItem 的項目值

繁花不似錦 2022-07-01 16:46:48
我對編碼很陌生,所以真的很掙扎,但我相信這很簡單。使用時Flatlist renderItem item_renderItem = ( item ) => {    return (<View style={{ flex: 1 }}>        <Text>{item}</Text>        <Text>{GLOBAL.products[item].title}</Text>    </View >)};render() {    return (        <View style={styles.list}>            <FlatList                data={[9,10]}                renderItem={ this._renderItem} />        </View>    )}工作正常,<Text>{item}</Text>首先渲染 9,然后渲染 10。但是這<Text>{GLOBAL.products[item].title}</Text>給了我錯誤:TypeError: TypeError: undefined is not an object (評估 '_global.default.products[item].title<Text>{GLOBAL.products[**{**item**}**].title}</Text>不起作用。還有_renderItem = ( **{**item**}** ) => {.<Text>{GLOBAL.products[9].title}</Text>作品很好。也試過GLOBAL.products[parseInt(item)].title
查看完整描述

1 回答

?
POPMUISE

TA貢獻1765條經驗 獲得超5個贊

很明顯你不知道 JavaScript 對象和子調用。當你調用一個孩子但它的父母不存在時,你肯定會在TextReact Native 的組件中遇到錯誤,你應該至少傳遞一個空字符串。


你應該防御性地培養孩子的連鎖呼喚。為此,我建議您閱讀有關可選鏈接的信息,并使用此鏈接將其添加到您的項目中。


安裝后可以編寫如下_renderItem函數:


_renderItem = item => (

  <View style={{ flex: 1 }}>

    <Text>{item}</Text>

    <Text>{GLOBAL?.products[item]?.title || ''}</Text>

  </View >

);

或者有一個更好的使用方法,通過使用orlodash.get安裝它,然后像下面這樣使用它:yarn add lodash.getnpm install --save lodash.get


import get from 'lodash.get';


~~~


_renderItem = item => (

  <View style={{ flex: 1 }}>

    <Text>{item}</Text>

    <Text>{get(GLOBAL, ['products', 'item', 'title'], '')}</Text>

  </View >

);

我更喜歡第二個。這種防御性編程可防止您的應用程序崩潰


查看完整回答
反對 回復 2022-07-01
  • 1 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號