0
私はグリッドに実装するAPIからイメージをロードしています。私のイメージは未定義であるため表示されません。私のコンポーネントのいくつかの機能があります。 ImagesとisLoadingはどちらもコンストラクタで空の配列とtrueに定義されています。データを正しく読み込む方法ネイティブ反応する
componentWillMount() {
api().then((res) => {
this.state.images.push(res);
}).done();
this._load10Images();
this.setState({isLoading:false}); // is loading is set to false
} // after images loaded.
_getImageUrls(){
api().then((res) => {
this.state.images.push(res);
console.log(this.state.images); //right here works array is growing
console.log(this.state.images[0]);//with image uris
}).done();
}
_load10Images(){
for(let i=0; i<10; i++){
this._getImageUrls(); //call _getImageUrls 10 times to fill array
}
}
_loadImageGrid(){
if(this.state.isLoading){
return <Text>Loaading ...</Text>
}
else{
console.log(this.state.images[0]); // coming back as undefined
return <Text>not loading ...</Text>
}
}
render(){ return <View>
{this._loadImageGrid()}
</View>
}
イメージ配列は、render関数と_loadImageGrid関数では定義されていません。
状態プロパティを変更するのは無意味です。新しいレンダーパスをトリガーするには、setStateを使用する必要があります。 – flq
@flq私はあなたが言っていることを見ていると思います。 –
haha、回答を参照してください:) – flq