Answer the question
In order to leave comments, you need to log in
What was wrong?
Tried to connect to wordpress using react and get data from it, but got TypeError: "this.state.title is undefined".
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries.
What are they connected with?
import React from 'react';
import './App.css';
class App extends React.Component {
constructor(props) {
super(props)
this.state = {
title: {},
date: "",
content: {}
};
}
componentDidMount() {
return fetch(`http://mysite.loc/wp-json/wp/v2/posts/`)
.then((response) => response.json())
.then((responseJson) => {
const { title, date, content } = responseJson;
this.setState({ title, date, content });
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<div>
<div>
<div className="row">
<div className="leftcolumn">
<div className="card">
<div className="title">
<h1>{this.state.title.rendered}</h1>
<p> {this.state.date} </p>
</div>
<div
className="content"
dangerouslySetInnerHTML={{ __html: this.state.content.rendered }} />
</div>
</div>
</div>
</div>
</div>
);
}
}
export default App;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question