我试图按照本教程设置AJAX请求。我已经完成了每一步,并安装了所有必要的东西,但是我得到了这个错误:
未明错误:目标容器不是DOM元素。
在这个文件中
import React from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';
class FetchDemo extends React.Component {
constructor(props) {
super(props);
this.state = {
posts: []
};
}
componentDidMount() {
axios.get(`http://www.reddit.com/r/${this.props.subreddit}.json`)
.then(res => {
const posts = res.data.data.children.map(obj => obj.data);
this.setState({ posts });
});
}
handleFieldChange(fieldId, value) {
console.warn(`${fieldId}: ${value}`)
this.setState({ [fieldId]: value });
}
render() {
return (
<div>
<h1>{`/r/${this.props.subreddit}`}</h1>
<ul>
{this.state.posts.map(post =>
<li key={post.id}>{post.title}</li>
)}
</ul>
</div>
);
}
}
ReactDOM.render(
<FetchDemo subreddit="reactjs"/>,
document.getElementById('root')
);下面是前面提到的教程:https://www.codeproject.com/Articles/1129563/AJAX-Requests-in-React-How-and-Where-to-Fetch-Data
谢谢你的帮助!
发布于 2018-09-24 23:38:07
<div id="root"></div>你需要在你的HTML代码中有这个
https://stackoverflow.com/questions/52488596
复制相似问题