简体   繁体   English

如何在 React Native 中从 flatlist 设置 Json 数组

[英]How to set Json array from flatlist in react native

I am new in react native .我是 react native 的新手。 i am get data from asynstorage.我从异步存储获取数据。 how to set that data from flatlist.如何从 flatlist 设置该数据。 my problem is can not set value properly in flatlist data.我的问题是无法在 flatlist 数据中正确设置值。 herewith below attached the JSon value and my code.下面附上 JSon 值和我的代码。 am try to long time but no idea for that please help me.我尝试了很长时间但不知道请帮助我。 please

I get data to asystorage value Json Data我将数据获取到 asystorage 值 Json 数据

    {
   "users":[
      {
         "title":"Opna Women's Short Sleeve Moisture",
         "image":"https://fakestoreapi.com/img/51eg55uWmdL._AC_UX679_.jpg",
         "id":19
      },
      {
         "title":"DANVOUY Womens T Shirt Casual Cotton Short",
         "image":"https://fakestoreapi.com/img/61pHAEJ4NML._AC_UX679_.jpg",
         "id":20
      }
   ]
}

Main code.主要代码。

   import React, { useState } from 'react';
import { SafeAreaView, StyleSheet, View, Text, Image, Alert, TouchableOpacity } from 'react-native';
import { FlatList } from 'react-native-gesture-handler';
import Icon from 'react-native-vector-icons/MaterialIcons';
import COLORS from '../../consts/colors';
import foods from '../../consts/foods';
import { PrimaryButton } from '../components/Button';
import AsyncStorage from '@react-native-async-storage/async-storage';

const CartScreen = ({ navigation }) => {
  const [value, setvalue] = useState(true);


  AsyncStorage.getItem('storeddata')
    .then((value) => {
      setvalue(value)
      console.log("saveddata", value);

    }
    )

  const CartCard = ({ item }) => {
    console.log("itemlist", item);
    return (
      <View style={style.cartCard}>
        <Image source={{ uri: item.image }} style={{ height: 80, width: 80 }} />
        <View
          style={{
            height: 100,
            marginLeft: 10,
            paddingVertical: 20,
            flex: 1,
          }}>
          <Text style={{ fontWeight: 'bold', fontSize: 16 }}>{item.title}</Text>
          <Text style={{ fontSize: 13, color: COLORS.grey }}>
            {item.title}
          </Text>
          <Text style={{ fontSize: 17, fontWeight: 'bold' }}>${item.id}</Text>
        </View>
        <View style={{ marginRight: 20, alignItems: 'center' }}>
          <Text style={{ fontWeight: 'bold', fontSize: 18 }}>5</Text>
          <View style={style.actionBtn}>
            <Icon name="remove" size={25} color={COLORS.white} />
            <Icon name="add" size={25} color={COLORS.white} />
          </View>
        </View>
      </View>
    );
  };
  return (
    <SafeAreaView style={{ backgroundColor: COLORS.white, flex: 1 }}>
      <View style={style.header}>
        <Icon name="arrow-back-ios" size={28} onPress={navigation.goBack} />
        <Text style={{ fontSize: 20, fontWeight: 'bold' }}>Cart</Text>
      </View>
      <FlatList
        showsVerticalScrollIndicator={false}
        contentContainerStyle={{ paddingBottom: 80 }}
        data={value}
        renderItem={({ item }) => <CartCard item={item} />}
        ListFooterComponentStyle={{ paddingHorizontal: 20, marginTop: 20 }}
        ListFooterComponent={() => (
          <View>
            <View
              style={{
                flexDirection: 'row',
                justifyContent: 'space-between',
                marginVertical: 15,
              }}>
              <Text style={{ fontSize: 18, fontWeight: 'bold' }}>
                Total Price
              </Text>
              <Text style={{ fontSize: 18, fontWeight: 'bold' }}>$50</Text>
            </View>
            <View style={{ marginHorizontal: 30 }}>
              <PrimaryButton title="CHECKOUT" onPress={() => navigation.navigate('Checkout')} />
            </View>
          </View>
        )}
      />
    </SafeAreaView>
  );
};
const style = StyleSheet.create({
  header: {
    paddingVertical: 20,
    flexDirection: 'row',
    alignItems: 'center',
    marginHorizontal: 20,
  },
  cartCard: {
    height: 100,
    elevation: 15,
    borderRadius: 10,
    backgroundColor: COLORS.white,
    marginVertical: 10,
    marginHorizontal: 20,
    paddingHorizontal: 10,
    flexDirection: 'row',
    alignItems: 'center',
  },
  actionBtn: {
    width: 80,
    height: 30,
    backgroundColor: COLORS.primary,
    borderRadius: 30,
    paddingHorizontal: 5,
    flexDirection: 'row',
    justifyContent: 'center',
    alignContent: 'center',
  },
});

export default CartScreen;

If your value have that above JSON data stored then try to pass prop like:如果您的value存储了上述JSON数据,请尝试传递如下道具:

<FlatList
    ...
    data={value.users}
    ...
/>

Hope this works for you.希望这对你有用。

Flatlist data props take an array as an input Official doc Flatlist data props 以数组为输入官方文档

you are getting an object from local storage you have to pass an array to it您正在从本地存储中获取一个对象,您必须将一个数组传递给它

local storage save string value so you have to do JSON.stringify() it when you store data in it and JSON.parse() it when getting data from local storage local storage link本地存储保存字符串值,因此当您将数据存储在其中时必须执行JSON.stringify()并在从本地存储本地存储链接获取数据时执行JSON.parse()

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

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