我有一个react组件,它在构造函数方法中以空白默认状态开头。 当componentDidMount我想用我通过AJAX从我的API获得的东西来更新状态。
此组件的代码如下所示:
import React from 'react';
import Header from './Header';
import FeedOwnerColumn from './FeedOwnerColumn';
import FeedColumnRight from './FeedColumnRight';
import ReportBug from './ReportBug';
class FeedApp extends React.Component{
constructor(){
super();
this.addComment = this.addComment.bind(this);
this.componentDidMount = this.componentDidMount.bind(this);
this.state = {
ownerInfo:{},
ownerDogs:[],
ownerApps:[],
ownerChat:{},
posts:[]
}
}
componentDidMount(e){
var that = this;
$.get('/api/getUserInit', function(result) {
console.log(result);
this.setState({
ownerInfo: result.ownerInfo,
ownerDogs: result.ownerDogs,
ownerNotifications: result.ownerNotifications,
ownerChat: result.ownerChat,
posts: result.posts
});
}.bind(this));
}
addComment(e){
e.preventDefault();
var currTime = new Date();
currTime = currTime.toLocaleString();
var commentContent = e.target.childNodes[0].value;
var key = Math.random();
key = Math.round(key);
var newComment = {
id: key,
name: "Peter Paprika",
date: currTime,
thumb_picture:"/imgs/user-q.jpg",
profile_link: "/user/123",
content: commentContent,
like_amount: 0
};
var postsObj = this.state.posts;
//console.log(postsObj[0].comments);
var newComments = postsObj[0].comments;
newComments.push(newComment);
console.log(newComments);
this.setState({posts: postsObj});
}
render() {
return (
<div className="clearfix wrapper">
<Header />
<FeedOwnerColumn />
<FeedColumnRight posts={this.state.posts} addComment={this.addComment} />
<ReportBug />
</div>
);
}
}
export default FeedApp;
不幸的是,this.setState({})
方法中的componentDidMount
似乎没有被正确调用,因为没有状态更新发生,我的地图函数期望数组无关,并返回错误。
我想在状态更新中使用的对象如下所示:
ownerChat: Object
ownerDogs: Array[1]
ownerInfo: Object
ownerNotifications: Array[0]
posts: Array[2]
我真的不知道在这种方法旁边还有什么可以尝试:/
答案 0 :(得分:3)
如果我理解正确,您需要在更新状态时重新呈现安装代码。 ComponentDidMount仅在组件初始化时触发。所有更新都通过ComponentDidUpdate触发。将该接口用于状态更新时将要发生的任何事情。
componentDidUpdate: function (prevProps, prevState) {
...
}
<强>更新强>
对不起,我正在用我的手机回答这个问题而且我觉得我没读不懂。如果你想在每次状态发生变化时进行ajax调用,你都会使用componentDidUpdate,这不是你要求的。
我注意到你在构造函数中重新声明了componentDidMount,它不应该存在,因为它已经被声明并且你要覆盖它。除了那段代码,我在这里看不出任何错误。 componentDidMount应该消失并正确更新。但是,我正在读到,当对象为空时,您的地图函数会出错。确保处理空状态,因为第一个绑定将为空,然后第二个绑定将具有数据。您的映射中的错误可能是此处的罪魁祸首。此示例可防止子节点绑定,直到ajax调用返回。
import React from 'react';
import Header from './Header';
import FeedOwnerColumn from './FeedOwnerColumn';
import FeedColumnRight from './FeedColumnRight';
import ReportBug from './ReportBug';
class FeedApp extends React.Component{
constructor(){
super();
// only adding functions not defined by super
this.addComment = this.addComment.bind(this);
this.state = {
hasData: false,
ownerInfo:{},
ownerDogs:[],
ownerApps:[],
ownerChat:{},
posts:[]
}
}
componentDidMount(e){
var that = this;
$.get('/api/getUserInit', function(result) {
console.log(result);
console.log('isMounted: ' + this.isMounted());
if (this.isMounted()) {
this.setState({
hasData: true,
ownerInfo: result.ownerInfo,
ownerDogs: result.ownerDogs,
ownerNotifications: result.ownerNotifications,
ownerChat: result.ownerChat,
posts: result.posts
});
}
}.bind(this));
}
addComment(e){
e.preventDefault();
var currTime = new Date();
currTime = currTime.toLocaleString();
var commentContent = e.target.childNodes[0].value;
var key = Math.random();
key = Math.round(key);
var newComment = {
id: key,
name: "Peter Paprika",
date: currTime,
thumb_picture:"/imgs/user-q.jpg",
profile_link: "/user/123",
content: commentContent,
like_amount: 0
};
var postsObj = this.state.posts;
//console.log(postsObj[0].comments);
var newComments = postsObj[0].comments;
newComments.push(newComment);
console.log(newComments);
this.setState({posts: postsObj});
}
render() {
var content = '';
if (this.state.hasData) {
content = (
<div>
<Header />
<FeedOwnerColumn />
<FeedColumnRight posts={this.state.posts} addComment={this.addComment} />
<ReportBug />
</div>
);
}
return (
<div className="clearfix wrapper">
{{ content }}
</div>
);
}
}
export default FeedApp;
请告诉我这是否适合您。