gpt4 book ai didi

javascript - 多次获取 react

转载 作者:行者123 更新时间:2023-12-03 03:11:21 25 4
gpt4 key购买 nike

我正在使用 fetch 方法在服务器上进行 API 调用。首先,我请求拥有一个对象数组( [{"id": 1, "name": "a", "userid": "12"},{"id": 2, "name": "b", "userid": "22"},{"id": 3, "name": "c", "userid": "23"}]

之后,我需要该对象的一个​​值来提供更多信息(在本例中,我需要 userid 来获取用户的信息)。

为此,我正在执行多个请求以显示所有内容。但是执行多个请求会给我一个错误。

Objects are not valid as a React child (found: object with keys {_45, _81, _65, _54}). If you meant to render a collection of children, use an array instead or wrap the object using createFragment(object) from the React add-ons. Check the render method of View

这是我的代码。第二次获取是在 renderImage 函数中。我设置为异步,但我知道这可能是错误的。

class Dashboard extends Component{

constructor(props){
super(props)
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2
}),
loaded: false,
datos: '',
}

}

componentDidMount(){
this.fetchData();
}

fetchData(){
const REQUEST_URL = "XXXXXXX";
fetch(REQUEST_URL)
.then((response) => response.json()) //la convertimos a json
.then ((responseData) =>{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
})
})
}

renderLoadingView(){
return(
<View>
<Text>Cargando...</Text>
</View>
)
}

async renderImage(userid){
const REQUEST_URL = "XXXXXXX" + userid;
const response = await fetch(REQUEST_URL);
const json = await response.json();
this.setState({loaded: true})
return (<Thumbnail source={{uri: json.imageUrl}} />)

}

renderReceta(receta){
return(
<Card>
<CardItem>
<TouchableOpacity onPress={()=> this.onUser(receta)}>
{this.renderImage(receta.user_id)}
</TouchableOpacity>
<Body>
<Text>{receta.Titulo}</Text>
<Text>{receta.Username}</Text>
</Body>
</CardItem>
</Card>
)
}

render(){

if(!this.state.loaded){
return this.renderLoadingView();
}
else{
return(
<Container>
<Header><Title>H</Title></Header>
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderReceta.bind(this)}
/>
</Container>
)
}

}

}

最佳答案

因此,一种可能的解决方案是为图像设置不同的状态。例如:

this.state = {
[...]
imageLoaded: false
}


fetchData(){
const REQUEST_URL = "XXXXXXX";
fetch(REQUEST_URL)
.then((response) => response.json()) //la convertimos a json
.then ((responseData) =>{
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData),
})
this.renderImage(); //Now you start loading the Image
})
}


renderImage(userid){
const REQUEST_URL = "XXXXXXX" + userid;
const response = await fetch(REQUEST_URL); //You can change this await and just load the fetch in background, does not matter right now.
const json = await response.json();
this.setState({imageLoaded: true, imageUrl: json.imageUrl})
}


renderReceta(receta){
let image = null;
if (this.state.imageLoaded)
image = (<Thumbnail source={{uri: this.state.image.imageUrl}} />);
return(
<Card>
<CardItem>
<TouchableOpacity onPress={()=> this.onUser(receta)}>
{image}
</TouchableOpacity>
<Body>
<Text>{receta.Titulo}</Text>
<Text>{receta.Username}</Text>
</Body>
</CardItem>
</Card>
)
}

这段代码不会立即工作,因为我有拼写错误,并且我删除了一些内容,但希望您能明白这一点。

PS。我想我之前回答过类似的问题,请考虑接受您的答案,这样它们就不会悬而未决。

关于javascript - 多次获取 react ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46937743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com