【问题标题】:React Native: map function in return [closed]React Native:返回映射函数[关闭]
【发布时间】:2019-03-19 17:47:45
【问题描述】:

我刚开始接触本机反应,我想知道如何在返回中处理地图函数。

根据此处Using react props in .map function 给出的答案,我将我的代码修改为以下内容:

render()
{
    const { active } = this.state

    const drawerContent = Object.keys(drawerItems).map(section => {
        return <DrawerComponent.Section key={section.toLowerCase()} title={section} style={styles.section}>
                {drawerItems[section].map(item =>
                    <DrawerComponent.Item
                        key={item.id}
                        label={item.label}
                        icon={item.icon}
                        style={styles.item}
                        active={active === item.id}
                        onPress={() => nav.push(item.screen + 'Screen', item.props || {})}
                    />
                )}
            </DrawerComponent.Section>
    })

    return(<View style={styles.container}>{drawerContent}</View>)
}

这是最好的方法还是可以更好地优化?

【问题讨论】:

    标签: reactjs optimization native


    【解决方案1】:

    我觉得不错。不过,我会考虑使用Object.entries 而不是Object.keys

    【讨论】:

    • 谢谢!虽然具有挑战性。不同的来源提出了不同的方法,例如这个站点medium.freecodecamp.org/…在render中使用了map函数,并在类中指定了一个自己的函数。
    猜你喜欢
    • 2017-03-20
    • 1970-01-01
    • 1970-01-01
    • 2022-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多