将 React Native 连接到 Express



我刚刚开始使用 React-Native。我正在使用博览会和快递。 我正在尝试将正面连接到背面并发出 GET 请求。 我得到一个 :

'RootErrorBondary':Error borders应该实现getDerivedStateFromError((。

在该方法中,返回 用于显示错误消息或回退 UI 的状态更新。

谢谢!

这是我的应用程序.js

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import axios from 'react-native-axios';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
greetings: [],
};
}
componentDidMount() {
axios.get('/api/v1').then(function (response) {
const greetings = response.data;
this.setState({ greetings });
console.log(greetings);
})
}
render() {
return (
<View style={styles.container}>
<Text>{greatings}</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
export default App;

不能直接在文本组件中显示数组。你来映射它。

render() {
return (
<View style={styles.container}>
{
this.state.greetings.map((item, index) => (
<Text key={index.toString()}>{item}</Text>
))
};
</View>
);
}

或者你可以只串起来,用文本组件显示它:

render() {
return (
<View style={styles.container}>
<Text>{JSON.stringify(this.state.greetings)}</Text>
</View>
);
}

请注意,状态变量只能通过this.state访问。您无法直接访问greeting

最新更新