Data API saya seperti ini

{"webimage":[],"appimage":[],"_id":"6155c2c77f31f9b79ba4273c","Categories":"Furniture","Subcategories":["Wooden Furniture","Office Furniture","Kitchen Furniture","Modular Office Furniture","Industrial Furniture","Steel Furniture","Living Room Furniture","|Storage and Display Furniture","Entryway Furniture","Inflatable Furniture","Patio Furniture & Accessories","Storage and Display Furniture"," Office & Commercial Furniture "]}

Saya ingin merender Semua Sub kategori Tetapi saya tidak dapat merender

T mencoba cara ini Tapi tidak berhasil

{val.Subcategories((item, key) => (
                  <Text key={key}>{item}</Text>
                ))}
0
Hand Code 1 Oktober 2021, 08:47

2 jawaban

Jawaban Terbaik

Inilah solusi yang berfungsi: Camilan Expo

Keluaran:

enter image description here

Kode Sumber Lengkap:

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

const value = {
  webimage: [],
  appimage: [],
  _id: '6155c2c77f31f9b79ba4273c',
  Categories: 'Furniture',
  Subcategories: [
    'Wooden Furniture',
    'Office Furniture',
    'Kitchen Furniture',
    'Modular Office Furniture',
    'Industrial Furniture',
    'Steel Furniture',
    'Living Room Furniture',
    '|Storage and Display Furniture',
    'Entryway Furniture',
    'Inflatable Furniture',
    'Patio Furniture & Accessories',
    'Storage and Display Furniture',
    ' Office & Commercial Furniture ',
  ],
};

export default function App() {
  return (
    <View style={styles.container}>
      {value.Subcategories.map((item, index) => (
        <Card style={{ margin: 2, padding: 5 }}>
          <Text>{item.trim()}</Text>
        </Card>
      ))}
    </View>
  );
}

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

1
Ketan Ramteke 1 Oktober 2021, 07:56

Nah, Anda salah, Anda harus menggunakan fungsi peta seperti di bawah ini:

{val.Subcategories.map((item, index) => (
    <Text key={`text-item-${index}`}>{item}</Text>
))}
1
ridvanaltun 1 Oktober 2021, 07:03