2017-03-31 11 views
1

私は少し問題がありますが、私は取得されているJSONデータを持っているし、データがレンダリングされます。しかしレンダリングでは、レンダリングしようとしているデータが最初のJSONに依存するため、別のフェッチを実行する必要があります。fetch内部からrender()の戻り値を取得していますか?

返された値を取得しようとしていますが、定義されていません。

したがって、このようなルックスフェッチ:

getFeaturedImage(thumbnail_json) { 
    fetch(thumbnail_json) 
     .then((response) => response.json()) 
     .then((responseData) => { 
     return responseData.media_details.sizes.medium.source_url; 
     }) 
     .done(); 
    } 

とrenderPostは()です:

renderPosts() { 
    contents = this.state.posts.map((post) => { 
     let postDate = Moment(post.date).format('LLL'); 

     if (post._links['wp:featuredmedia']) { 
     console.log('has featured image'); 
     let thumbnail_json = post._links['wp:featuredmedia'][0].href; 
     this.getFeaturedImage(thumbnail_json); 
     console.log(this.getFeaturedImage(thumbnail_json)); // <----- UNDEFINED? 
     } else { 
     console.log('no featured image'); 
     } 

     return (
     <View key={post.id} style={ postStyles.postContainer }> 
      <Text style={postStyles.postTitle}> 
      {post.title.rendered} 
      </Text> 
      <Text style={postStyles.postDate}> 
      {postDate} 
      </Text> 
      <View style={ postStyles.excerptContainer }> 
      <HTMLView stylesheet={htmlStyles} 
       value={post.excerpt.rendered} 
      /> 
      </View> 
     </View> 
    ); 
    }); 
    return (
     <ScrollView> 
     <View style={baseStyles.container}> 
      {contents} 
     </View> 
     </ScrollView> 
    ); 
    } 

答えて

1

あなたの機能はあなたのフェッチの前にリターンを追加し、現在は何も返しません。

getFeaturedImage(thumbnail_json) { 
    return fetch(thumbnail_json) 
    .then((response) => response.json()) 
    .then((responseData) => { 
    return responseData.media_details.sizes.medium.source_url; 
    }) 
    .done(); 

}

0

あなたが引数としてgetFeaturedImageにコールバック関数を渡し、success

mycallback(result) { 
    //do what you want with result 
} 
getFeaturedImage(thumbnail_json, callback) { 
    fetch(thumbnail_json) 
     .then((response) => response.json()) 
     .then((responseData) => { 
     callback(responseData.media_details.sizes.medium.source_url); 
     }) 
     .done(); 
    } 

renderPosts() { 
    contents = this.state.posts.map((post) => { 
     let postDate = Moment(post.date).format('LLL'); 

     if (post._links['wp:featuredmedia']) { 
     console.log('has featured image'); 
     let thumbnail_json = post._links['wp:featuredmedia'][0].href; 
     this.getFeaturedImage(thumbnail_json, this.mycallback); 
     console.log(this.getFeaturedImage(thumbnail_json)); // <----- UNDEFINED? 
     } else { 
     console.log('no featured image'); 
     } 

     return (
     <View key={post.id} style={ postStyles.postContainer }> 
      <Text style={postStyles.postTitle}> 
      {post.title.rendered} 
      </Text> 
      <Text style={postStyles.postDate}> 
      {postDate} 
      </Text> 
      <View style={ postStyles.excerptContainer }> 
      <HTMLView stylesheet={htmlStyles} 
       value={post.excerpt.rendered} 
      /> 
      </View> 
     </View> 
    ); 
    }); 
    return (
     <ScrollView> 
     <View style={baseStyles.container}> 
      {contents} 
     </View> 
     </ScrollView> 
    ); 
    } 
+0

にそれを呼び出すことができますそれが表示され、私は別の解決策を発見した、実際に使用したAPIイムはちょうど私ができることを意味し、すべてのデータを出力し、他のJSONを持っています単一の要求を行う。 – narutoramen

関連する問題