ReactJS,我想在ComponentDidMount中重新触发ajax调用
-我有一个组件(我们称之为StorageComponent),它从restful api获取数据(JSON)(请求是在componentDidMount中发出的) -然后,StorageComponent将数据传递给子组件,它将与其他组件一起显示数据并与用户交互ReactJS,我想在ComponentDidMount中重新触发ajax调用,reactjs,Reactjs,-我有一个组件(我们称之为StorageComponent),它从restful api获取数据(JSON)(请求是在componentDidMount中发出的) -然后,StorageComponent将数据传递给子组件,它将与其他组件一起显示数据并与用户交互 -现在有另一个独立于上述组件的层次结构 -这将处理用户的一些表单输入,每个表单输入有一个组件(单选按钮、复选框、文本等)。因为每次重新渲染都会消除子对象的任何状态,所以我必须使用对象文本(称为ModelObject)来存储每个表单输入
-现在有另一个独立于上述组件的层次结构 -这将处理用户的一些表单输入,每个表单输入有一个组件(单选按钮、复选框、文本等)。因为每次重新渲染都会消除子对象的任何状态,所以我必须使用对象文本(称为ModelObject)来存储每个表单输入。因此,每当用户在表单中输入某个内容时,它都会调用Modelobject并将其存储在表单中,组件也会从Modelobject请求数据 -在用户输入所有输入之后,他最终将点击这个层次结构中的submit按钮组件,在该组件中,它将调用ModelObject向RestAPI发送ajax POST。我的问题是,我希望ModelComponent再次从RestAPI获取数据,这样用户将看到更新的数据。我认为forceUpdate()会起作用,我认为它会重新触发渲染,从而在StorageComponent中安装componentDidMount 那么,最好的方法是什么呢。此外,是否有上述不良做法?这些信息足够吗 编辑: storageComponent层次结构
var StorageComponent= React.createClass({
getInitialState: function(){
return {
data: []
};
},
componentDidMount: function(){
this.serverRequest = $.get(this.props.source, function(result){
result = JSON.parse(result);
this.setState({
data: result
});
}.bind(this));
},
render: function() {
return (
<div>
<Nav dataList={this.state.data} /> //whole bunch of other child component below this one
</div>
);
}
});
app.storageComponent= React.render(
<HabitModel source = "/api/listing/user"/>,
document.getElementById('myDiv')
);
我想我们需要看到一个淡化的plunker/codepen来了解组件的结构。我添加了一些代码,这足够好吗?我省略了很多东西,因为我认为这只会浪费你的阅读时间。我认为我们需要看一个淡化的plunker/codepen来了解你的组件的结构。我添加了一些代码,这足够好吗?我遗漏了很多东西,因为我认为这会浪费你的时间去阅读。
var formModel = {
newInfo: {
inputBox: "",
frequency: "",
date: "",
days: []
},
addDescription: function(description){
this.newHabitInfo.description = description;
},
addFrequency: function(selection){
this.newHabitInfo.frequency = selection;
},
addDay: function(startDay){
this.newHabitInfo.startDay = startDay;
},
getFrequency: function(){
return this.newHabitInfo.frequency;
},
//this is the function I want the second hierarchy of components to
//use to force the storageComponent to do the re-trigger the ajax
updateHabitListing: function(){
if(this.validate()){
app.habitListing.forceUpdate();
}else{
console.log("form not finish");
}
}