[英]WordPress with React: Saving and Using Data Collected with fetch
I am trying to do a hopefully 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. 我正在尝试做一个有希望的基本事情,即react,它访问由本地安装的WordPress网站创建的终结点,以便我可以使用该数据并以自己喜欢的方式呈现它。
I am trying to set the state to the data but alsough it can be printed to the console in the componentWillMount()
function, the state posts
remains empty. 我想国家规定的数据,但alsough它可以打印在控制台componentWillMount()
函数,状态posts
仍然空。 I can console.log
the data from that function but I cannot set the state and then use it in the render
function. 我可以使用console.log
该函数中的数据,但是无法设置状态,然后在render
函数中使用它。 My code is below: 我的代码如下:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
export default class Widget extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentWillMount() {
const theUrl = "http://localhost:8888/test-site/wp-json/wp/v2/posts";
fetch(theUrl)
.then(response => response.json())
.then(response =>
this.setState({
posts: response
})
)
}
render() {
console.log('render posts: ' + this.state.posts);
return (
<div>
<h1>React Widget</h1>
<p>posts:</p>
</div>
);
}
}
Widget.propTypes = {
wpObject: PropTypes.object
};
Console: 安慰:
JQMIGRATE: Migrate is installed, version 1.4.1
react-dom.development.js:21258 Download the React DevTools for a better development experience: https://.me/react-devtools
react-dom.development.js:21258 Download the React DevTools for a better development experience: https://.me/react-devtools
Widget.jsx:28 render posts:
jquery.loader.js:2 running on http://localhost:8888/test-site/
Widget.jsx:28 render posts: [object Object],[object Object],[object Object],[object Object]
the second to last line in the console has a collapse arrow next to it and I can see that those are in fact the posts with all of their correct information. 控制台的倒数第二行旁边有一个折叠箭头,我可以看到它们实际上是带有所有正确信息的帖子。 Why can I not set the state to the data being returned by fetch()
? 为什么不能将状态设置为fetch()
返回的数据?
React has a special function setState()
to set a components state (for class components). React有一个特殊的函数setState()
来设置组件状态(对于类组件)。 So instead of a direct assignment 因此,而不是直接分配
this.state = {
value: 'foo2',
posts: data.value,
};
use 采用
this.setState({
value: 'foo2',
posts: data.value,
})
. 。
This results in the following code. 这将导致以下代码。 Also, your fetch belongs into the componentDidMount()
function (I missed that in my first draft). 另外,您的提取属于componentDidMount()
函数(我在初稿中未填写)。
export default class Widget extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
fetch("http://localhost:8888/test-site/wp-json/wp/v2/posts")
.then(data => data.json())
.then(data => this.setState({posts: data.value}))
}
render() {
return (
<div>
<p>value: {this.state.posts}</p>
</div>
);
}
}
For this special example, you might also want to use functional components with a useState()
and a useEffect
hook: 对于此特殊示例,您可能还希望将功能组件与useState()
和useEffect
挂钩一起使用:
export default function Widget() {
const [posts, setPosts] = useState([]);
useEffect(() => {
fetch("http://localhost:8888/test-site/wp-json/wp/v2/posts")
.then(data => data.json())
.then(data => setPosts(data.value))
});
return (
<div>
<p>value: {posts}</p>
</div>
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.