使用 React Native 时,如何保存获取的结果?
When Using React Native, how do you save the results of a fetch?
我目前正在编写一个 Android React Native 应用程序,该应用程序由 JSON api 通过 Ruby 在 Rails 服务器上提供服务.我目前最大的障碍是保存 fetch
调用的结果。我的代码如下:
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
的异步性质,但我不确定如何解决这个问题。
如有任何帮助,我们将不胜感激!
这是保存和访问 api 调用结果的方法之一。调用 api 的最佳方式是从 componentWillMount 生命周期。这个生命周期在组件被渲染之前被调用。
您可以直接在 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);
我目前正在编写一个 Android React Native 应用程序,该应用程序由 JSON api 通过 Ruby 在 Rails 服务器上提供服务.我目前最大的障碍是保存 fetch
调用的结果。我的代码如下:
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
的异步性质,但我不确定如何解决这个问题。
如有任何帮助,我们将不胜感激!
这是保存和访问 api 调用结果的方法之一。调用 api 的最佳方式是从 componentWillMount 生命周期。这个生命周期在组件被渲染之前被调用。
您可以直接在 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);