2016-07-28 64 views
0

我目前正在編寫Android React Native應用程序,該應用程序由通過Ruby on Rails服務器提供的JSON API支持。我目前最大的困難是保存了fetch電話的結果。我的代碼如下:使用React Native時,如何保存提取結果?

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    TouchableHighlight, 
    Alert 
} from 'react-native'; 

class MiniMinionClient extends Component { 
    constructor(props) { 
    super(props); 
    } 

    getStatus() { 
    return fetch('http://10.0.2.2:3000/api/v1/status.json') 
     .then((response) => response.json()) 
     .then((responseJson) => { 
     return responseJson; 
     }) 
     .catch((error) => { 
     console.error(error); 
     }); 
    } 

    render() { 
    var status = this.getStatus() 
    return (
     <View style={styles.container}> 
     <Text>Version {status.version}</Text> 
     <Text>Last Update: {status.last_update}</Text> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
     flex: 1, 
     flexDirection: 'row', 
     justifyContent: 'center', 
     alignItems: 'center', 
     backgroundColor: '#F5FCFF', 
    } 
}); 

AppRegistry.registerComponent('MiniMinionClient',() => MiniMinionClient); 

我不知道這是一個有效的端點,因爲我能夠做同樣的工作隨叫隨到,如果我用警惕的帖子,我計上心來從here。我認爲這個問題源於fetch的異步性質,但我不知道如何解決這個問題。

任何幫助將不勝感激!

+0

你能提供'responseJson'的輸出還是json文件的數據?如果我知道格式,我可以爲您提供完整的工作代碼。 –

+0

作爲函數結果返回的值是一個Promise對象,並且json文件的數據是 { 「version」:「0.0.1」, 「last_update」:「07/27/2016 at 06:15 AM「 } – Riizu

回答

1

以下是保存和訪問api調用結果的方法之一。調用API的最佳方式是從componentWillMount生命週期。這個生命週期在Component呈現之前被調用。

您可以直接在componentWillMount()上使用您的api調用,也可以調用您創建的getStatus()函數。

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View, 
    TouchableHighlight, 
    Alert 
} from 'react-native'; 

class MiniMinionClient extends Component { 
    constructor(props) { 
    super(props); 
    // Initialize empty state here 
    this.state = { 
     version: '', 
     last_update: '' 
    }; 
    } 
    componentWillMount() { 
    // It's best to use your api call on componentWillMount 
    this.getStatus(); 
    } 

    getStatus() { 
    fetch('http://10.0.2.2:3000/api/v1/status.json') 
     .then((response) => response.json()) 
     .then((responseJson) => { 
     // set the state of the output here 
     this.setState({ 
      version: responseJson.version, 
      last_update: responseJson.last_update 
     }); 
     }) 
     .catch((error) => { 
     console.error(error); 
     }); 
    } 

    render() { 
    return (
     <View style={styles.container}> 
     {/*You can now access the values using this.state here*/} 
     <Text>Version {this.state.version}</Text> 
     <Text>Last Update: {this.state.last_update}</Text> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    flexDirection: 'row', 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    } 
}); 

AppRegistry.registerComponent('MiniMinionClient',() => MiniMinionClient); 
+0

非常感謝!那就是訣竅。我對JS,Async和React Native的經驗不足使得這有點難度。我感謝您的支持。 – Riizu

+0

雖然做更多的研究,我也來到這個帖子以及[這裏](http://stackoverflow.com/questions/37035423/react-native-fetch-api-not-returning-my-calls)。 這一個使用componentDidMount()與componentWillMount(),這些是相同的,還是它們提供不同的好處? – Riizu

+0

您應該詳細瞭解** React生命週期**。這是你必須意識到的最重要的話題之一。 這兩者之間存在差異: ** componentWillMount **在渲染之前在服務器和客戶端執行。 **僅在客戶端首次渲染後執行componentDidMount **。 這些鏈接很有幫助:http://javascript.tutorialhorizo​​n.com/2014/09/13/execution-sequence-of-a-react-components-lifecycle-methods/ http://www.tutorialspoint.com/reactjs /reactjs_component_life_cycle.htm –

相關問題