在我的应用程序中,我试图从我的 api 获取数据。我已经尝试在我的其他模块中获取数据,它们都工作正常,但这里不行。
在这里,我试图在我的 api 中获取单个对象/数据。
这是我的代码
分类.js
export default class Category extends Component {
constructor(props){
super(props)
this.state = {
data: [],
orderDet: '',
};
}
fetchDataOrderNo = async () => {
const response = await fetch("http://192.168.254.105:3308/OrderNo/order_no")
const json = await response.json()
this.setState({ orderDet: json })
}
componentDidMount() {
this.fetchDataOrderNo();
}
render() {
return (
<View>
<View style={{flexDirection: 'row'}}>
<Text>Table No: { this.state.orderDet }</Text>
</View>
</View>
)
}
}
最佳答案
您将获得一个数组作为对您请求的响应。您必须访问数组中的第一个对象,并获取 order_no
键:
fetchDataOrderNo = async () => {
const response = await fetch("http://192.168.254.105:3308/OrderNo/order_no")
const json = await response.json()
this.setState({ orderDet: json[0].order_no })
}
关于javascript - React Native - 不变违规 : Objects are not valid as a React child,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51233392/