Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 正在从render()内部的获取中检索返回值?_Reactjs_React Native - Fatal编程技术网

Reactjs 正在从render()内部的获取中检索返回值?

Reactjs 正在从render()内部的获取中检索返回值?,reactjs,react-native,Reactjs,React Native,我有点麻烦,我有JSON数据正在被提取,然后数据被呈现。但是,在呈现中,我需要执行另一次获取,因为我试图呈现的数据依赖于第一个JSON 您可以看到我正在尝试获取返回值,但它是未定义的 因此,提取过程如下所示: getFeaturedImage(thumbnail_json) { fetch(thumbnail_json) .then((response) => response.json()) .then((responseData) => {

我有点麻烦,我有JSON数据正在被提取,然后数据被呈现。但是,在呈现中,我需要执行另一次获取,因为我试图呈现的数据依赖于第一个JSON

您可以看到我正在尝试获取返回值,但它是未定义的

因此,提取过程如下所示:

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)=>{
让postDate=时刻(post.date).format('LLL');
如果(发布链接['wp:featuredmedia']){
log('has featured image');
让缩略图_json=post._链接['wp:featuredmedia'][0].href;
this.getFeaturedImage(缩略图_json);

console.log(this.getFeaturedImage(thumbnail_json));//如果函数当前未返回任何内容,请在获取之前添加一个返回:

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

}

您可以在
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>
    );
  }
mycallback(结果){
//对结果做你想做的
}
getFeaturedImage(缩略图、json、回调){
获取(缩略图和json)
.then((response)=>response.json())
.然后((响应数据)=>{
回调(responseData.media\u details.size.medium.source\u url);
})
.完成();
}
renderPosts(){
contents=this.state.posts.map((post)=>{
让postDate=时刻(post.date).format('LLL');
如果(发布链接['wp:featuredmedia']){
log('has featured image');
让缩略图_json=post._链接['wp:featuredmedia'][0].href;
this.getFeaturedImage(缩略图,this.mycallback);

console.log(this.getFeaturedImage(thumbnail_json));//看起来,我找到了另一个解决方案,我使用的api实际上有另一个json输出所有数据,这意味着我可以只发出一个请求。
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>
    );
  }