简体   繁体   English

FlatList 不呈现来自 json 数组 React-native 的数据

[英]FlatList not rendering data from json array React-native

I have a FlatList in React Native -我在 React Native 中有一个 FlatList -

Here's my object that I'm passing on to FlatList.这是我要传递给 FlatList 的 object。

    const [workoutSet, setWorkoutSet] = useState([
        { setNum: 1, reps: 10, weight: 0 },
        { setNum: 2, reps: 10, weight: 0 },
    ]);

    <SetList data={workoutSet} />

Within SetList I'm trying to render the data passed on to here.在 SetList 我试图呈现传递给这里的数据。

export default function SetList(data) {
    return (
        <>
            <FlatList
                data={data}
                renderItem={(item) => {
                    <Text style={styles.name}> {item} </Text>;
                }}
            />
            {console.log(data.data[0].reps)}
        </>
    );
}

console.log(data.data[0].reps ) returns 10, as expected.正如预期的那样, console.log(data.data[0].reps ) 返回 10。

However, if I do a console.log inside renderItem, it doesn't print anything.但是,如果我在 renderItem 中执行 console.log,它不会打印任何内容。 I'd like to access setNum, reps and weight inside renderItem so that I can display a list.我想访问 renderItem 中的renderItem setNum, reps and weight以便显示列表。 What am I doing wrong?我究竟做错了什么? I've searched StackOverflow and couldn't find an answer to this.我搜索了 StackOverflow,但找不到答案。 Thanks.谢谢。

Working example items in your render is an object thats why it is not showing text component don't show object as text i recommend reading about flatlist to understand more about it -key extractors -layouts渲染中的工作示例项目是 object 这就是它不显示文本组件的原因 不将 object 显示为文本 我建议阅读有关 flatlist 以了解更多信息 -key extractors -layouts

like best practices since it can be a problem for large lists (very slow performance for 100 rows)喜欢最佳实践,因为它可能是大型列表的问题(100 行的性能非常慢)

working example https://snack.expo.io/LpItecGOc工作示例https://snack.expo.io/LpItecGOc

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

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