简体   繁体   English

ReactJS显示来自Wordpress API的数据

[英]ReactJS display data from Wordpress API

I am new to React so I am just trying to pull data from my wordpress website API. 我是React的新手,所以我只是想从我的wordpress网站API中提取数据。 I am getting a generic blog post which will display this.state.post.link fine but not any of the rendered data. 我正在获得一个通用博客文章,该文章将很好地显示this.state.post.link但不会显示任何呈现的数据。

import React from 'react'

export default React.createClass({

  getInitialState: function () {
    return {
      post: {},
    }
  },

  componentDidMount: function () {
    var _this = this;
    $.get('http://somewebsite.net/wp-json/wp/v2/posts/1258', function(result) {
      _this.setState({
        post: result
      });
    });
  },

  render() {
    console.log(this.state.post);
    return <div className="single-post">
      <h1>{this.state.post.link}</h1>
      <h1>{this.state.post.title.rendered}</h1>
    </div>
  }
});

I get this error from adding post.title.rendered . 我从添加post.title.rendered得到此错误。

bundle.js:51835 Uncaught TypeError: Cannot read property 'rendered' of undefined

This is what shows with the code console.log(this.state.post.title); 这就是代码console.log(this.state.post.title);

Object {rendered: "Example post"}

So why can I console.log this.state.post.title and it shows the object with rendered in but then if I try and display that it will say title is undefined? 那么,为什么我可以console.log this.state.post.title并显示带有渲染的对象,但是如果我尝试显示它会说title是未定义的呢?

Define the initial state for title.rendered as a empty string. 定义title.rendered的初始状态。 title.rendered为空字符串。

this.state = {
  post: {
    title: {
      rendered: ''
    }
  }
...
render() {
  return (
    <div>
      {this.state.post.title.rendered}
    </div>
  )
}

OR 要么

Check if the state is defined before rendering: 在渲染之前检查状态是否已定义:

render() {
  return (
    <div>
      {this.state.post ? this.state.post.title.rendered : null }
    </div>
  )
}

Reason is you are fetching the data from api, until you didn't get the data, this.state.post.title will be undefined , and you are trying to access rendered of undefined , that's why it is throwing the error: 原因是您正在从api获取数据,直到没有获取数据为止, this.state.post.title将是undefined ,并且您试图访问undefined rendered ,这就是它this.state.post.title错误的原因:

Cannot read property 'rendered' of undefined 无法读取未定义的“渲染”属性

ajax call is asynchronous call, it will take time to fetch the data and render method will get called before that. ajax调用是asynchronous调用,将需要一些时间来获取数据,并且在此之前将调用render方法。

One solution is, put the check on this.state.post.title : 一种解决方案是,将检查放在this.state.post.title

render() {
    console.log(this.state.post);
    return <div className="single-post">
      <h1>{this.state.post.link}</h1>
      <h1>{this.state.post.title && this.state.post.title.rendered}</h1>
    </div>
  }

Or hold the complete rendering until you didn't get the data, by putting the check on this.state.post inside render method. 或者通过将检查放在render方法内的this.state.post上,保留完整的渲染,直到没有数据为止。

Update - 更新 -

Define the initial value of post as null : 将post的初始值定义为null

getInitialState: function () {
    return {
      post: null,
    }
  },

Then check the value of post inside render method, it will not rendering anything until you didn't get the response: 然后检查render方法内部的post值,直到您没有得到响应,它才会渲染任何内容:

render() {
    console.log(this.state.post);

    if(!this.state.post) return null;

    return <div className="single-post">
      <h1>{this.state.post.link}</h1>
      <h1>{this.state.post.title.rendered}</h1>
    </div>
}

Note : There is one issue with this, rendered key must be present inside title in response otherwise it will also throw the same error. 注意 :与此有关的一个问题是, rendered键必须出现在title以作为响应,否则也会引发相同的错误。

maybe this will be helpful: 也许这会有所帮助:

render() {
    console.log(this.state.post);
    var link = (this.state.post && this.state.post.link) ? this.state.post.link : '';
    var rendered = (this.state.post && this.state.post.title && this.state.post.title.rendered) ? this.state.post.title.rendered : '';

    return <div className="single-post">
      <h1>{link}</h1>
      <h1>{rendered}</h1>
    </div>
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM