0
      import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
  TouchableOpacity,
  Image,
  TextInput,                                            
  TouchableHighlight,                                            
  Alert
} from 'react-native';

import Button from 'react-native-button'
import {Actions} from 'react-native-router-flux'
import Home from './Home'

export class Weight extends Component{
    constructor(props) {
  super(props);

  this.state = {
    data: '',
    data1:'',
    textinput:'',
    entryDate: '',
    systol:''
  }

  componentDidMount(){
    this._onPressButtonGET();
  } 
 _onPressButtonPOST(){
            fetch("url", {
                method: "POST",
                 headers: {
                     'Accept': 'application/json',
                     'Content-Type': 'application/json',
                 },
                body: JSON.stringify({
                    "entryDate":"3/2/2017 2:00 AM",
                    "systol": "120",
                    "mobileType":"ANDROID",
                    "userName":"menutest"

                })})
            .then((response) => response.json())
            .then((responseData) => {
                Alert.alert(
                    "Blood pressure data",
                    "Blood pressure data - " + JSON.stringify(responseData)
                )
            })
            .done();
        }

        _onPressButtonGET ()  {
            fetch("url", {
                method: "POST",
                 headers: {
                     'Accept': 'application/json',
                     'Content-Type': 'application/json',
                 },
                body: JSON.stringify({"mobileType":"ANDROID","userName":"menutest"})})
            .then((response) => response.json())
            .then((responseData) => {
                    this.setState({ data: JSON.stringify(responseData) })
                })

            .done();
        }
    render(){
            return(
                <View>
                    <TouchableHighlight onPress={this._onPressButtonPOST}>
                        <Text>Add</Text> 
                    </TouchableHighlight>


                     <TouchableHighlight onPress={this._onPressButtonGET.bind(this)}>
                        <Text>show</Text>
                       </TouchableHighlight>

                      <Text>{this.state.responseData.entryDate}</Text>
                      <Text>{this.state.responseData.systol}</Text>
                       <Text>hello{this.state.data}</Text> 

                </View>
            );
        }
    }

    module.exports = Weight;

I want to display only systol and entryDate on the screen, but my above code is displaying all the data from web services, How can i edit my code that i want to display only those things? i tried with this.setState({ data: responseData.entryDate }) but this is not working need help. and how to use for loop in _onPressButtonGET() to display all entry dates and systol.

1 Answer 1

1

If response data is an object instead of

this.setState({ data: JSON.stringify(responseData) })

use

this.setState({ entryDate: responseData.entryDate, systol: responseData.systol })

and then in the render use {this.state.entryDate} and {this.state.systol}

First make sure you have a clean code in the constructor (add } after setting the state), then make sure you fetch data from the correct url and not just a string, then make sure you receive the exact data that you need and that is a object, then set the info you need on state as I described above, then use it in the render function.

Sign up to request clarification or add additional context in comments.

10 Comments

undefined is not an object (evaluating 'this.state.responsedata.entryData') this is error i am getting.
I edited my answer! Make sure you store and update on state only what you need and the keys from the state object are matching the keys used in render.
add some console.logs before setting the state like this: console.log(typeof responseData) console.log(Object.keys(responseData)) before this.setState({ entryDate: responseData.entryDate, systol: responseData.systol })
how can i use for loop here? actually i have many entryDate and systol?
console.log() is not making any difference
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.