【问题标题】:How do i get data that return from another component react js我如何获取从另一个组件返回的数据反应 js
【发布时间】:2021-01-26 12:30:12
【问题描述】:

我需要获取一个 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问题,但我不知道如何修改代码。

任何帮助将不胜感激!

【问题讨论】:

    标签: javascript reactjs react-native async-await


    【解决方案1】:

    您可以将响应保存在数组状态中,然后在您需要的地方调用并修改它。

    类似这样的:

    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 中显示条目,也可以像使用状态一样修改它们。

    【讨论】:

    • 或者 res.data 如果你需要存储的话
    【解决方案2】:

    修改你的函数如下。

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

    【讨论】:

    • 第 2 行返回时显示“意外令牌”错误
    • 能否提供这条路由的后端定义http://myapi.com/API/fetch_all
    猜你喜欢
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2020-04-03
    • 2019-03-01
    • 2018-07-19
    • 1970-01-01
    相关资源
    最近更新 更多