无法在本机反应中获取数据数组
failed to get array of data in react native
我正在为我的应用程序使用本机 CRNA,但我无法从 API 加载数据数组。
这是我的代码:
constructor(props){
super(props);
this.state ={
branches: [],
loading: false,
}
}
componentDidMount() {
fetch('APIurl', {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
branches: responseJson.data,
loading: true,
})
console.log(responseJson);
return responseJson;
})
.catch(error=>{
console.log(error);
});
}
}
render(){
return(
<Text style={{margin: 2, alignItems: 'stretch',
flex:1, fontWeight: 'bold'}}>
{this.state.branches.name}</Text>
<Text style={{margin: 2, justifyContent: 'space-
around', alignItems: 'stretch', flex:2,}}>
{this.state.branches.address}</Text>
<Picker
selectedValue={this.state.branches.name}
mode="dropdown"
style={{ height: 50, justifyContent: 'space-
around', flex:4, alignItems: 'stretch', }}
onValueChange={(itemValue, itemIndex)=>
this.setState({branches: key})}>
{this.state.branches.map((branches, key)=> (
<Picker.Item label={branches.name} value={branches.name} key=
{branches.branch_id}/>)
)}
</Picker>
<View style={{flex: 5, flexDirection:'column', alignItems: 'stretch',justifyContent: 'space-around'}}>
<Button
title="GO"
titleStyle={{ fontWeight: "700" }}
buttonStyle={{
backgroundColor: "#5eac1a",
width: 250,
height: 45,
borderColor: "transparent",
borderWidth: 0,
borderRadius: 25
}}
/>);
}
}
当我尝试 运行 时,代码没有显示任何错误。但是它没有显示任何东西,就像它无法从 API 中获取任何数据一样。谁能帮帮我吗?非常感谢..
编辑:它没有显示任何错误,而是显示警告。
Each child in an array or iterator should have a unique "key" prop.
内部响应json你可以这样做。
responsejson.forEach((item)=>{
this.state.branches.push({
branchid:item.branch_id
})
})
- 此 branch_id 来自 json 数据类型。
我正在为我的应用程序使用本机 CRNA,但我无法从 API 加载数据数组。
这是我的代码:
constructor(props){
super(props);
this.state ={
branches: [],
loading: false,
}
}
componentDidMount() {
fetch('APIurl', {
method: 'GET',
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
branches: responseJson.data,
loading: true,
})
console.log(responseJson);
return responseJson;
})
.catch(error=>{
console.log(error);
});
}
}
render(){
return(
<Text style={{margin: 2, alignItems: 'stretch',
flex:1, fontWeight: 'bold'}}>
{this.state.branches.name}</Text>
<Text style={{margin: 2, justifyContent: 'space-
around', alignItems: 'stretch', flex:2,}}>
{this.state.branches.address}</Text>
<Picker
selectedValue={this.state.branches.name}
mode="dropdown"
style={{ height: 50, justifyContent: 'space-
around', flex:4, alignItems: 'stretch', }}
onValueChange={(itemValue, itemIndex)=>
this.setState({branches: key})}>
{this.state.branches.map((branches, key)=> (
<Picker.Item label={branches.name} value={branches.name} key=
{branches.branch_id}/>)
)}
</Picker>
<View style={{flex: 5, flexDirection:'column', alignItems: 'stretch',justifyContent: 'space-around'}}>
<Button
title="GO"
titleStyle={{ fontWeight: "700" }}
buttonStyle={{
backgroundColor: "#5eac1a",
width: 250,
height: 45,
borderColor: "transparent",
borderWidth: 0,
borderRadius: 25
}}
/>);
}
}
当我尝试 运行 时,代码没有显示任何错误。但是它没有显示任何东西,就像它无法从 API 中获取任何数据一样。谁能帮帮我吗?非常感谢..
编辑:它没有显示任何错误,而是显示警告。
Each child in an array or iterator should have a unique "key" prop.
内部响应json你可以这样做。
responsejson.forEach((item)=>{
this.state.branches.push({
branchid:item.branch_id
})
})
- 此 branch_id 来自 json 数据类型。