简体   繁体   English

React Native - 从 api 渲染数据时“未定义不是对象”

[英]React Native - 'undefined is not an object' when rendering data from api

I am making an application that takes data from API and displays it on the screen.我正在制作一个从 API 获取数据并将其显示在屏幕上的应用程序。 When the screen is rendered it gives the following error: Render Error undefined is not an object (evaluating 'this.text').渲染屏幕时,会出现以下错误:未定义的渲染错误不是 object(正在评估“this.text”)。 funny thing is nothing like 'this.text' is in any of my screens except for a file in node modules.有趣的是,除了节点模块中的文件之外,我的任何屏幕中都没有像“this.text”这样的东西。 Anyway, here is the file无论如何,这是文件

the screen that renders the data:呈现数据的屏幕:

 /* the screen that renders the data */ import React, {useEffect, useState} from 'react'; import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native'; const Experts = () => { useEffect(()=> { fetch('http://icantputtheactualurlcuzitsmyipaddress.com/expertsList/',{ method: 'GET' }).then(resp => resp.json).then(data => { setData(data) }).catch(error => console.log('something is not suppose to be happening')) }, []) const [data, setData] = useState([]); return ( <SafeAreaView style={styles.container}> <FlatList data={data} renderItem={({ item }) => ( <Text style={styles.item}>{item.name}</Text> )} /> </SafeAreaView> ); } const styles = StyleSheet.create({ container: { flex: 1, marginTop: StatusBar.currentHeight || 0, }, item: { backgroundColor: 'teal', padding: 20, marginVertical: 8, marginHorizontal: 16, color: 'white', }, title: { fontSize: 32, }, }); export default Experts;

i'd truly appretiate any input, im kinda new to this.我真的很感激任何输入,我对此有点陌生。

I think your data array is empty 1st time before API fetch and Text inside your FlatList is trying to fetch so provide null checking by "?"我认为您的数据数组在 API 提取之前第一次为空,并且您的 FlatList 中的文本正在尝试提取,因此请通过“?”提供 null 检查like item?.name喜欢项目?.name

so after some research i found out it was because the call for the api in the effect hook was a function.所以经过一番研究,我发现这是因为效果挂钩中对 api 的调用是 function。 and you need parenthesis for functions.你需要括号来表示函数。 Also, the reason it showed this.text was because it was under the json parser.此外,它显示 this.text 的原因是因为它位于 json 解析器下。 so to the part you've been waiting for所以到你一直在等待的部分

the code:编码:

 useEffect(()=> { fetch('http://skrskr.com/api/experts/',{ method: 'GET' }) /* make sure to put the needed parenthesis */.then((resp) => resp.json()).then( (json) => setData(json) ).catch(error => console.log('something is not suppose to be happening')) }, []) const [data, setData] = useState([]);

i hope this finds who needs it我希望这能找到需要它的人

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

相关问题 渲染时获取 undefined 不是 React native 中的对象 - Getting undefined is not an object in React native when rendering React Native - 渲染从 API 获取数据的屏幕时出现“未定义不是对象”错误 - React Native - “Undefined is not an object” error when render a screen that take data from API 渲染数据,从 react-native 的 API 中获取 - Rendering data, fetched from an API in react-native 使用来自 API 的数据在 React Native 中进行条件渲染 - Using data from an API for conditional rendering in react native React Native 中的第一个数据中来自 API responseJson 的未定义数据 - Undefined data from API responseJson in First data in React Native React Native 错误:将数据传递给组件时,未定义不是 object - React Native Error: undefined is not an object when passing data to component 从React Native导入StyleSheet时,未定义不是对象 - Undefined is not object when importing StyleSheet from React Native 通过API调用渲染图像-React Native - Image rendering from API Call - React Native React Native:“未定义不是对象”使用ListView和外部dataSource渲染组件 - React Native: “undefined is not an object” Rendering components with ListView and external dataSource Undefined 不是 object React native - 但之后显示数据 - Undefined is not an object React native - but displays data afterwards
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM