2017-03-06 77 views
0
_onPressButtonPOST(){ 
     fetch("url", { 
      method: "POST", 
      headers: { 
       'Accept': 'application/json', 
       'Content-Type': 'application/json', 
      }, 
      body: JSON.stringify({"entryDate":"3/2/2017 2:00 AM","dia":"808","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) => { 
      Alert.alert(
       "Blood pressure data", 
       "Blood pressure data - " + JSON.stringify(responseData) 
      ) 
    }) 
     .done(); 
    } 



    render(){ 
     return(
      <View> 


      <TouchableHighlight onPress={this._onPressButtonPOST} > 
        <Text>Add</Text> 
       </TouchableHighlight> 
       <TouchableHighlight onPress={this._onPressButtonGET} > 
        <Text>show</Text> 
       </TouchableHighlight> 


      </View> 
      ); 
    } 

我能值存儲到數據庫並獲取他們回來,但我在警告對話框顯示這些值,但我想在屏幕上顯示,如何編輯我的代碼來顯示entryDate和dia在屏幕上,每當我點擊顯示按鈕?在屏幕上顯示的值,而不是在警告框

回答

1

使用state -

constructor(props) { 
    super(props); 

    this.state = { 
    data: '' 
    }; 
} 

_onPressButtonGET =() => { 
    .... 
    .then((responseData) => { 
    this.setState({ 
     data: JSON.stringify(responseData) 
    }) 
    }) 
} 

render() { 
    return(
    <View> 
     ..... 
     ..... 
     <Text>{this.state.data}</Text> 
    </View> 
); 
} 
+0

越來越像「錯誤未定義不是一個函數(評價「this2.setState({數據:responseData})) – Prasanna

+0

變化'_onPressButtonGET(){''到_onPressButtonGET =() => {'。脂肪箭頭功能自動綁定'this'。 – vinayr

+0

這是不工作,通過點擊顯示什麼也沒有發生。 – Prasanna