javascript - React Native - 不变违规:对象作为 React 子项无效
问题描述
在我的应用程序中,我试图从我的 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 })
}
推荐阅读
- autodesk-viewer - restoreState 保持先前查看器状态的剖切面
- c# - EF Core:方法“ProcessModelFinalized”没有实现
- jquery - 如何使 Select2 在生产中与 rails 6 应用程序一起使用?
- sql - SQL 选择并显示包含至少一个值的组中的所有值
- java - 既然我们可以直接使用setter方法访问Java中的私有变量,为什么还要使用getter方法呢?
- database - 将 DatabaseModule 导入 App.Module 时 NestJS 微服务未实例化
- javascript - ZonedDateTime 对象在后端接收的问题
- python - 解析我的 json 时出现问题,我得到这个“JSONDecodeError: Invalid \escape”
- python - base64编码图像;binascii.Error: 无效的 base64 编码
- php - PHP 7.4.1 开发服务器 - 双重请求