简体   繁体   English

了解 renderItem 属性中的解构<flatlist />在 React Native 中

[英]understanding destructuring in renderItem property in <FlatList /> in React Native

I came across the following React Native code online:我在网上遇到了以下 React Native 代码:

import React, { useState } from 'react';
import { StyleSheet, Text, View, FlatList } from 'react-native';

export default function App() {
  const [people, setPeople] = useState([
    { name: 'shaun', id: '1' },
    { name: 'yoshi', id: '2' },
    { name: 'mario', id: '3' },
    { name: 'luigi', id: '4' },
    { name: 'peach', id: '5' },
    { name: 'toad', id: '6' },
    { name: 'bowser', id: '7' },
  ]);

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

      <FlatList 
        numColumns={2}
        keyExtractor={(item) => item.id} 
        data={people} 
        renderItem={({item}) => ( 
          <Text style={styles.item}>{item.name}</Text>
        )}
      />

    </View>
  );
}

I do not quite following the snippet related to the renderItem property.我不太关注与renderItem属性相关的代码段。 why are we using the destructuring syntax {item} .为什么我们使用解构语法{item} Also this is an object destructuring syntax (using curly braces), but in object destructuring we use keys as variable names.这也是一个 object 解构语法(使用花括号),但在 object 解构中,我们使用键作为变量名。 So I am afraid I am completley at a loss to understand the snippet below.所以恐怕我完全无法理解下面的片段。

<FlatList 
        numColumns={2}
        keyExtractor={(item) => item.id} 
        data={people} 
        renderItem={({item}) => ( 
          <Text style={styles.item}>{item.name}</Text>
        )}

Any guidance would be appreciated.任何指导将不胜感激。

this...这个...

renderItem={({item}) => ( 
  <Text style={styles.item}>{item.name}</Text>
)}

actually mean....其实意思....

renderItem={(props) => ( 
  <Text style={styles.item}>{props.item.name}</Text>
)}

This technique is called object destruction check out for more info - https://medium.com/podiihq/destructuring-objects-in-javascript-4de5a3b0e4cb这种技术称为 object 破坏检查以获取更多信息 - https://medium.com/podiihq/destructuring-objects-in-javascript-4de5a3b0e4cb

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

相关问题 React Native 中的 FlatList 实现 - renderItem 功能不清楚? - 未定义的属性 - FlatList implementation in React Native - renderItem functionality unclear? - undefined property (React Native) - 传入参数<flatlist renderitem></flatlist> - (React Native) - Pass parameter in <FlatList renderItem> 反应 FlatList 渲染项 - React FlatList renderItem 将额外的道具数据传递给 react-native-draggable-flatlist 中的 renderItem - pass extra props data to renderItem in react-native-draggable-flatlist 如何在 react native 和 TS 中输入 prop 收到的 renderItem Flatlist? - How to typing renderItem Flatlist received by prop in react native and TS? React Native:renderItem 组件到 Custom FlatList 作为道具? (如 Vue 插槽) - React Native: renderItem component to Custom FlatList as a prop? (like Vue slots) 为什么我们需要在 React Native 中的 FlatList 的 renderItem() 方法中使用大括号 - Why we need braces in renderItem() method for FlatList in React Native React Native的FlatList属性renderItem是否需要额外的“ item”(项目)=&gt; {item.item.name}? - React Native's FlatList prop renderItem requires extra “item” (item) => {item.item.name}? FlatList 的 renderItem 应该用 react useCallback 钩子包装吗? - Should renderItem of FlatList be wrapped with react useCallback hook? React Native - 如何在 FlatList 属性上使用导航? - React Native - How to use navigation on FlatList property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM