admin管理员组文章数量:1420907
I am trying to do a basic thing with react, which is access an endpoint created by my locally installed WordPress website so that I can use that data and render it in a way I like.
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class Widget extends Component {
constructor(props) {
super(props);
var data = fetch("http://localhost:8888/test-site/wp-json/wp/v2/posts")
.then(data => data.json())
.then(data => {
console.log(data);
// this.state = {
// value: 'foo2',
// posts: data.value,
// };
})
this.state = {
value: 'foo2',
posts: data.value,
};
}
render() {
return (
<div>
<p>value: {this.state.posts}</p>
</div>
);
}
}
Widget.propTypes = {
wpObject: PropTypes.object
};
I am trying to set the state to the data but it is returned as a promise. Apparently I can console.log
the data but I cannot use it in the render
function. You can see that I try to set the state in the second .then()
but when I uncomment that and delete the thing below it everything stops working. If I cannot set the state inside the .then()
how do I use the returned data in the render()
function?
本文标签: plugin developmentWordPress with React Saving and Using Data Collected with fetch
版权声明:本文标题:plugin development - WordPress with React: Saving and Using Data Collected with fetch 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745345717a2654467.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论