简体   繁体   English

反应原生中的地图功能

[英]map function in react native

i want to use a map function to display the label but i get an error like "Cannot read property 'label' of undefined" in TypeError: Cannot read property 'label' of undefined"我想使用地图函数来显示标签,但我在 TypeError 中收到类似“无法读取未定义的属性‘标签’的错误:无法读取未定义的属性‘标签’”

I'm using radio-buttons-react-native to do this.我正在使用 radio-buttons-react-native 来做到这一点。 it has an option: data and it accepts an array of data.它有一个选项:data,它接受一个数据数组。 This is how it works.这就是它的工作原理。 So for my code, the right way is to do this data = {data} .所以对于我的代码,正确的方法是做这个data = {data} The reason why I want to use the map function is because I want to get the data from my server side.我之所以要使用map函数,是因为我想从我的服务器端获取数据。 and it seems nothing is working似乎没有任何效果

 import * as React from 'react'; import { useState } from 'react'; import { Button, View, Text, Image, SafeAreaView, ScrollView, } from 'react-native'; import RadioButtonRN from 'radio-buttons-react-native'; import { Card, Title } from 'react-native-paper'; import Icon from 'react-native-vector-icons/FontAwesome'; import Constants from 'expo-constants'; function App() { const [data, setData] = useState([{ label: 'data 1' }, { label: 'data 2' }, ]); const [SecondData, setSecondData] = useState([{ label: 'data 5' }, { label: 'data 6' }, { label: 'data 7' }, { label: 'data 8' }, ]); const keeper = data.map((item) => { < Text > { item.label } < /Text> console.log(item.label) }) return ( < SafeAreaView > < ScrollView showsVerticalScrollIndicator > < View style = { { marginTop: Constants.statusBarHeight } } > < Card mode = "outlined" style = { { borderColor: 'blue', padding: 10, backgroundColor: '#ccc', } } > < Card.Content > < Title > SRC PRESIDENT < /Title> < /Card.Content> < RadioButtonRN data = { keeper } selectedBtn = { (e) => console.log(e) } icon = { < Icon name = "stop-circle" size = { 25 } color = "#2c9dd1" / > } /> < /Card> < Card mode = "outlined" style = { { borderColor: 'blue', marginTop: 20, padding: 10, backgroundColor: '#ccc', } } > < Card.Content > < Title > GENERAL SECRETARY < /Title> < /Card.Content> < RadioButtonRN data = { SecondData } selectedBtn = { (e) => console.log(e) } icon = { < Icon name = "check-circle" size = { 25 } color = "#2c9dd1" / > } /> < /Card> < /View> < /ScrollView> < /SafeAreaView> ); } export default App;

first of all your implementation of.map is wrong it will not work in react.首先你的.map 实现是错误的,它不会在反应中工作。 I guess it should return the jsx like this.我想它应该像这样返回 jsx。 eg.例如。

const keeper = data.map((item)=>(<Text>{item.label}</Text>))

and from this error msg "TypeError: Cannot read property 'label' of undefined"并从此错误消息“TypeError:无法读取未定义的属性'标签'”

it means the RadioButtonRN component requires an array of object data with label property.这意味着 RadioButtonRN 组件需要具有标签属性的对象数据数组。 and here you are passing jsx inside the array.在这里,您将在数组中传递 jsx。 so it will not work.所以它不会工作。

so I would suggest you instead of mapping over this data and passing jsx just pass a plain object with label property.所以我建议你不要映射这些数据并传递 jsx 只是传递一个带有标签属性的普通对象。

you need to create a state in hook and fetch data (use promise or async/await) and update this state.您需要在 hook 中创建一个状态并获取数据(使用 promise 或 async/await)并更新此状态。 and your updated state data will go inside RadioButtonRN component.并且您更新的状态数据将进入 RadioButtonRN 组件。

use conditional rendering to hide data while fetching or pass some default entries在获取或传递一些默认条目时使用条件渲染来隐藏数据

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

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