Javascript React本机-不变冲突:对象作为React子对象无效
在我的应用程序中,我试图从api中获取数据。我已经尝试在我的其他模块中获取数据,它们都工作得很好,但这里不行 在这里,我试图在api中获取单个对象/数据 这是我的密码 Category.jsJavascript React本机-不变冲突:对象作为React子对象无效,javascript,android,reactjs,react-native,fetch,Javascript,Android,Reactjs,React Native,Fetch,在我的应用程序中,我试图从api中获取数据。我已经尝试在我的其他模块中获取数据,它们都工作得很好,但这里不行 在这里,我试图在api中获取单个对象/数据 这是我的密码 Category.js export default class Category extends Component { constructor(props){ super(props) this.state = { data: [], orde
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>
)
}
}
导出默认类类别扩展组件{
建造师(道具){
超级(道具)
此.state={
数据:[],
orderDet:“”,
};
}
fetchDataOrderNo=async()=>{
const response=等待获取(“http://192.168.254.105:3308/OrderNo/order_no")
const json=await response.json()
this.setState({orderDet:json})
}
componentDidMount(){
this.fetchDataOrderNo();
}
render(){
返回(
表号:{this.state.orderDet}
)
}
}
您将获得一个数组作为对请求的响应。您必须访问数组中的第一个对象,并获取order\u 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 })
}