你可以这样做。只是不要忘记在您的 JSX 组件之前放置“返回”。
例子:
render() {
if(this.state.page === 'news') {
return <Text>This is news page</Text>;
} else {
return <Text>This is another page</Text>;
}
}
从互联网获取数据的示例:
import React, { Component } from 'react';
import {
View,
Text
} from 'react-native';
export default class Test extends Component {
constructor(props) {
super(props);
this.state = {
bodyText: ''
}
}
fetchData() {
fetch('https://example.com').then((resp) => {
this.setState({
bodyText: resp._bodyText
});
});
}
componentDidMount() {
this.fetchData();
}
render() {
return <View style={{ flex: 1 }}>
<Text>{this.state.bodyText}</Text>
</View>
}
}