Meteor 和 React - Componentdidmount,多个 Meteor.calls 和 setstate
Meteor and React - Componentdidmount, multiple Meteor.calls and setstate
我正在尝试通过 meteor 调用获取特定的用户详细信息(出于安全原因需要在服务器端获取它们)并将结果设置为状态。我现在正在尝试的是:
componentDidMount(){
Meteor.call('getCurrentCF', this.state.currentAccount, (err, res) => {
if (!err) {
var result = JSON.parse(res.content);
if (typeof result.cfu['destinations'] != 'undefined') {
if(result.cfu['destinations'][0].destination === "voicebox") {
this.setState({
cfActive: 'btn btn-block btn-maik',
vbActive: 'btn btn-block btn-maik-active',
});
} else {
this.setState({
cfActive: 'btn btn-block btn-maik-active',
vbActive: 'btn btn-block btn-maik',
currentCFDest: "(" + result.cfu['destinations'][0].simple_destination + ")",
});
}
} else {
this.setState({
cfActive: 'btn btn-block btn-maik',
vbActive: 'btn btn-block btn-maik',
});
}
} else {
console.log("Error: " + err);
}
});
Meteor.call('getCurrentVM', this.state.currentAccount, (err, res) => {
if(!err) {
var vms = JSON.parse(res.content);
this.setState({
vmCount: vms.total_count,
});
} else {
console.log(err)
}
});
this.setState({
isLoading: false,
});
}
但是在尝试此操作时,我收到一条控制台错误消息:
Warning: Can only update a mounted or mounting component. This usually
means you called setState, replaceState, or forceUpdate on an
unmounted component. This is a no-op.
一切正常,但它正在向浏览器控制台发送我不想要的错误消息。使它以正确的方式工作的正确方法是什么?
当您的回调在卸载组件后解析时,可能会发生这种情况。为避免这种行为,您必须跟踪您的组件是否仍处于挂载状态。这可以通过在装载时将标志设置为 true
然后在卸载时将标志设置为 false
来完成。 More info here.
我正在尝试通过 meteor 调用获取特定的用户详细信息(出于安全原因需要在服务器端获取它们)并将结果设置为状态。我现在正在尝试的是:
componentDidMount(){
Meteor.call('getCurrentCF', this.state.currentAccount, (err, res) => {
if (!err) {
var result = JSON.parse(res.content);
if (typeof result.cfu['destinations'] != 'undefined') {
if(result.cfu['destinations'][0].destination === "voicebox") {
this.setState({
cfActive: 'btn btn-block btn-maik',
vbActive: 'btn btn-block btn-maik-active',
});
} else {
this.setState({
cfActive: 'btn btn-block btn-maik-active',
vbActive: 'btn btn-block btn-maik',
currentCFDest: "(" + result.cfu['destinations'][0].simple_destination + ")",
});
}
} else {
this.setState({
cfActive: 'btn btn-block btn-maik',
vbActive: 'btn btn-block btn-maik',
});
}
} else {
console.log("Error: " + err);
}
});
Meteor.call('getCurrentVM', this.state.currentAccount, (err, res) => {
if(!err) {
var vms = JSON.parse(res.content);
this.setState({
vmCount: vms.total_count,
});
} else {
console.log(err)
}
});
this.setState({
isLoading: false,
});
}
但是在尝试此操作时,我收到一条控制台错误消息:
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.
一切正常,但它正在向浏览器控制台发送我不想要的错误消息。使它以正确的方式工作的正确方法是什么?
当您的回调在卸载组件后解析时,可能会发生这种情况。为避免这种行为,您必须跟踪您的组件是否仍处于挂载状态。这可以通过在装载时将标志设置为 true
然后在卸载时将标志设置为 false
来完成。 More info here.