简体   繁体   English

无法在本机反应中呈现来自 API 的数据

[英]Can't Render Data From API in react native

i want to render data from API, but when i want to view the data in render can't appear here my code, i got my error the data soal_id is undefined.我想渲染来自 API 的数据,但是当我想查看渲染中的数据时我的代码不能出现在这里,我得到了数据 soal_id 未定义的错误。 How to make appear the data with the true way?如何使数据以真实的方式出现? when i try to show the another data this is same doesn't appear and showing error.当我尝试显示另一个数据时,没有出现相同的数据并显示错误。

 import React, { Component } from 'react'; import { View, Text } from 'react-native'; // import { cos } from 'react-native-reanimated'; export default class QuizTes extends Component { constructor(props) { super(props); this.state = { soal: [], index:0, }; } getSoal =()=>{ fetch('http://unnamed48.ccug.gunadarma.ac.id:9091/api/user/soal',{ method:'post', headers:{ 'Accept': 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify({ token:"4266ae6eddc02afa38fa39477c22ab5f", studi:"penalaran umum" }) }).then(res => res.json()).then(getData =>{ // console.log(getData) if(getData){ this.setState({ soal: getData.data }) } }).catch(error =>{ console.log(error) }) } componentDidMount(){ this.getSoal() } nextSoal = ()=>{ this.setState({index:(this.state.index + 1) % this.state.soal.length}) } prevSoal = ()=>{ this.setState({index:(this.state.index - 1) % this.state.soal.length}) } render() { const item = this.state.soal[this.state.index] // var items = JSON.parse(item) // console.log(items) return ( <View> <Text> QuizTes </Text> <Text> {JSON.stringify(item).soal_id}/20 </Text> {/* <Text> {item.soal_pertanyaan} </Text> */} </View> ); } }

Dont use JSON.stringify in your case.不要在你的情况下使用JSON.stringify In the render method, just make a check on item and then render在渲染方法中,只需检查item然后渲染

Like this像这样

<Text> {item && item.soal_id}/20 </Text>

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

相关问题 从 API 获取数据时重新渲染 React Native Flatlist - Re render React Native Flatlist on data fetch from API React Native 无法获取分页数据并出现渲染错误 - React native can't featch pagination data and getting render error 无法使用路由器和 WordPress API 从列表中渲染出单页数据 - Can't render out single page data from list in a react application using router and WordPress API 无法使用 Redux-saga 作为我的中间件在 React Native 中接收来自 API 的数据 - Can't receive data from API in React Native using Redux-saga as my middleware 在本机反应中渲染来自 JSON 的数据 - Render data from JSON in react native Angular 2无法通过API调用呈现数据 - Angular 2 can't render data from API call React Native - 渲染从 API 获取数据的屏幕时出现“未定义不是对象”错误 - React Native - “Undefined is not an object” error when render a screen that take data from API context api - useEffect 在第一次渲染时不会触发 - 反应原生 - context api - useEffect doesn't fire on first render - react native React Native CameraRoll.getPhotos API 不呈现结果 - React Native CameraRoll.getPhotos API doesn't render the results API成功获取数据后如何在React Native中调用render - How to call render in React Native after API successfully fetches data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM