簡體   English   中英

map 數組內部反應原生功能組件

[英]map an array inside react native functional component

我正在嘗試在屏幕上打印我的字母數組,但目前沒有顯示任何內容。

我還嘗試將 touchableOpacity 視圖包裝為另一個視圖或將其更改為僅視圖或文本,但我還沒有成功。

const letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];

const OptionalLetters = props => {

    const mixedLetters = () => {

        letters.map(()=> (letter, key) => {
            return (
                <TouchableOpacity key={Math.random()} onPress={()=> console.log('letter pressed')}>
                    <Text>{letter}</Text>
                </TouchableOpacity>
            )
        })
    }

    return (
        <View style={styles.screen}>
            {mixedLetters()}
        </View>
    )
}

您不會退回 map。 做這個:

const letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m'];

const OptionalLetters = props => {

    const mixedLetters = () => {

        return letters.map((letter, key) => {
            return (
                <TouchableOpacity key={Math.random()} onPress={()=> console.log('letter pressed')}>
                    <Text>{letter}</Text>
                </TouchableOpacity>
            )
        })
    }

    return (
        <View style={styles.screen}>
            {mixedLetters()}
        </View>
    )
}

您的代碼中有一個額外的() =>

改變

letters.map(()=> (letter, key) => {

letters.map((letter, key) => {

您還可以使用key (它保存索引,因此它始終是唯一的)作為鍵

看起來您在這里忘記了退貨聲明letters.map

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM