简体   繁体   English

map 数组内部反应原生功能组件

[英]map an array inside react native functional component

I'm trying to print My letters array on the screen and nothing is currently showing.我正在尝试在屏幕上打印我的字母数组,但目前没有显示任何内容。

I've also tried to wrap the touchableOpacity view another View also or change it to View or Text only but I didn't succeeded yet.我还尝试将 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>
    )
}

You're not returning the map.您不会退回 map。 Do this:做这个:

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>
    )
}

You have an extra () => in your code.您的代码中有一个额外的() =>

Change改变

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

To

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

You can also use the key (which holds the index so it's always unique) as the key您还可以使用key (它保存索引,因此它始终是唯一的)作为键

It looks like you forgot a return statement here letters.map看起来您在这里忘记了退货声明letters.map

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM