0

365体育足球滚球比分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. 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>
    );
  }
}

  • JSON.stringify(item).soal_id ? is it correct? – jinongun May 28 at 3:47
  • i got error when i use that code. When i use item.soal_id i got the same error the soal_id is undefined. – Shyfa Andiantono May 28 at 3:57
0
0

Dont use JSON.stringify in your case. In the render method, just make a check on item and then render

Like this

<Text> {item && item.soal_id}/20 </Text>
| improve this answer | |
  • wow that's amazing, the value is showing now, thank you for helping. I'm sorry for my bad english. – Shyfa Andiantono May 28 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.