Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
55 views
in Technique[技术] by (71.8m points)

javascript - Unable to render array of objects in react-native

I have array of objects in React-Native that look like this in console

[{
  "isDonor": true,
  "name": "Nadi",
  "photo": "https://gre",
  "uid": "2ZE"
}, {
  "email": "mmaz",
  "isDonor": true,
  "name": "Mz",
  "photo": "https://gra",
  "uid": "Cb"
}]

but when i render it i get notthing on screen here is code

{donorsData.map((v, i) => {return <Text key={i}>{v.name}</Text>;})}

I'm trying to render each object on screen with it's properties

question from:https://stackoverflow.com/questions/66059878/unable-to-render-array-of-objects-in-react-native

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Looks perfect, it should render the data without any problem, but still here is the full working example:

Working App: Expo Snack

enter image description here

import * as React from 'react';
import { Text, View, StyleSheet, Image } from 'react-native';
import Constants from 'expo-constants';

const donorsData = [
  {
    isDonor: true,
    name: 'Nadi',
    photo: 'https://i.stack.imgur.com/t8vJf.jpg?s=328&g=1',
    uid: '2ZE',
    email: '[email protected]',
  },
  {
    email: '[email protected]',
    isDonor: true,
    name: 'Mz',
    photo: 'https://i.stack.imgur.com/t8vJf.jpg?s=328&g=1',
    uid: 'Cb',
  },
];
export default function App() {
  return (
    <View style={styles.container}>
      {donorsData.map((v, i) => {
        return (
          <View
            key={v.uid}
            style={{
              backgroundColor: 'white',
              padding: 10,
              margin: 5,
              borderRadius: 10,
            }}>
            <Text>{v.name}</Text>
            <Text>{v.email}</Text>
            <Image source={{ uri: v.photo }} style={{ height: 150, flex: 1 }} />
          </View>
        );
      })}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
});


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...