[英]connecting react native to express
我剛剛開始使用 React-Native。 我正在使用世博會和快遞。 我正在嘗試將前面連接到后面和一個 GET 請求。 我得到一個:
'RootErrorBondary':錯誤邊界應該實現 getDerivedStateFromError()。
在該方法中,返回狀態更新以顯示錯誤消息或 fallbackUI。
謝謝你!
這是我的 App.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;
您不能在 Text 組件中直接顯示數組。 你來映射吧。
render() {
return (
<View style={styles.container}>
{
this.state.greetings.map((item, index) => (
<Text key={index.toString()}>{item}</Text>
))
};
</View>
);
}
或者你可以只用字符串化並用 Text 組件顯示它:
render() {
return (
<View style={styles.container}>
<Text>{JSON.stringify(this.state.greetings)}</Text>
</View>
);
}
請注意,狀態變量只能通過this.state
訪問。 您不能直接訪問greeting
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.