繁体   English   中英

我如何获取从另一个组件返回的数据 react js

[英]How do i get data that return from another component react js

我需要获取一个 api 并从 React Native 的一个文件中获取这些数据,我可以通过 console.log() 获取获取 api 的文件中的数据

API.js


export const merchastListFetch = [

  axios.get('http://myapi.com/API/fetch_all')
  .then(function (response) {
    // console.log(response.data);
    //able to get the data here
    return response.data;
  })
]

Merchant_list.js


import React from 'react'
import { StyleSheet, Text, View } from 'react-native'
import MerchantItem from './MerchantItem'
import { FlatList, TouchableOpacity } from 'react-native-gesture-handler'
import { merchant } from '../../models/models'
import SectionHeader from './SectionHeader'
import { H4, P } from '../typography'
import { merchastListFetch } from '../../API/api'
//get the api from the file

export default function MerchantList({navigation}) {
    const renderItem = ({item})=> <MerchantItem {...item}/>
    console.log({merchastListFetch});
    //cannot access the data here
    //get the data like this {"merchastListFetch": [{"_U": 0, "_V": 1, "_W": [Object], "_X": null}]}

    return (
        <View style={styles.sectionContainer}>
            {/* <View style={styles.headerContainer}>
                <H4 style={styles.textTitle}>Nearby Merchants</H4>
                <View style={styles.flexContainer}>
                    <P style={styles.textDescription}>Pick from here get the fastest delivery</P>
                    <TouchableOpacity onPress={() => navigation.navigate('MerchantCategoryScreen')}>
                        <P style={styles.textLink}>See All</P>
                    </TouchableOpacity>
                </View>
            </View> */}
            <SectionHeader title="Nearby Merchants" description="Pick from here get the fastest delivery" onLinkPress={() => navigation.navigate('MerchantCategoryScreen')}/>
            <FlatList
                keyExtractor={(item)=>item.merchant_id}
                data={merchant}
                renderItem={renderItem}
                // itemDimension={80}
            />
        </View>
    )
}

我对 Merchant_list.js 的期望是像我在 API.js 中获得的数据

这是这样的格式

{"status":true,"data":[{"shop_id":"1","merchant_id":"1","area_id":"0","agent_id":"1","business_type_id":"1","business_category_id":"1","bill_type_id":"1","currency_id":"0","status_id":"0","register_name":"Dummy Name","register_no":"123456789","retail_name":"Test Only","description":"TESTING USE","commission":"20.00","gst_no":"12345","coming_soon":"0","is_halal":"0","is_active":"1","delivery_charge":"3.50","remarks":"TESTING USE AGAIN","approved_date":"0000-00-00 00:00:00","deleted":"0","created_date":"2020-10-06 15:02:20","created_by":"1","modified_date":"2020-10-08 09:37:53","modified_by":"1","merchant":"Merchant","shop_image":[{"shop_image_id":"3","shop_id":"1","file":"\/images\/shop\/5af16e1c6554160a79bea005.png","file_size":"65124","file_type":"image\/png","is_default":"1","is_active":"1","deleted":"0","created_date":"2020-10-09 13:21:23","created_by":"0","modified_date":"0000-00-00 00:00:00","modified_by":"0"}]},

我在网上做了一些研究,发现它可能是 aysnc 和 await 问题,但我不知道如何修改代码。

任何帮助将不胜感激 !

你可以将响应保存在一个数组状态中,然后在你需要的地方调用和修改它,我猜。

像这样的东西:

async CallApiFunc(){
await fetch("http://myapi.com/API/fetch_all")
.then(res => res.json())
.then(res => {
     console.log(JSON.stringify(res))
     this.setState({
     dataArray: res
})
})
.catch(err => console.log(err))
}

这需要一个dataArray:[]作为状态。

之后,您可以在 FlatList 中显示条目,也可以像处理状态一样修改它们。

修改你的函数如下。

export  const  merchastListFetch = ()=>{ 
return axios.get('http://myapi.com/API/fetch_all')
    .then(function (response) {
          return response.data;
    }).catch((error)=>{
        console.log(error)
    })
}

暂无
暂无

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

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