Javascript 难以在承诺中设定状态
我在componentDidMount函数中有一系列承诺。在values.thenfunctionresult承诺中,我试图设置状态。我未处理此错误拒绝类型错误:无法读取未定义的属性“setState”,但已研究此错误,我需要使用箭头函数绑定我的承诺,以使其能够访问this关键字 问题是,我不知道在这种承诺上把箭头函数放在哪里。无论我把它放在哪里,我都会得到一个错误,说Unexpected token,expected{,然后当我试着把花括号放进去时,我会得到另一个错误Javascript 难以在承诺中设定状态,javascript,reactjs,Javascript,Reactjs,我在componentDidMount函数中有一系列承诺。在values.thenfunctionresult承诺中,我试图设置状态。我未处理此错误拒绝类型错误:无法读取未定义的属性“setState”,但已研究此错误,我需要使用箭头函数绑定我的承诺,以使其能够访问this关键字 问题是,我不知道在这种承诺上把箭头函数放在哪里。无论我把它放在哪里,我都会得到一个错误,说Unexpected token,expected{,然后当我试着把花括号放进去时,我会得到另一个错误 componentDid
componentDidMount() {
let general = {};
let values = [];
//gets selected setting form schema
getSettingsForms().then((response) => {
this.setState({settingsForms: response});
general = response[this.state.selectedSetting];
}).then(response => {
values = getSettingsConfig(this.state.selectedSetting, general);
values.then(function(result) => {
this.setState({
formSchema: result[1],
selectedSetting: this.state.selectedSetting,
settings: result[0]
})
})
})
}
将此箭头函数中的值设置为value.thenfunctionresult promise的正确方法是什么,这样我就可以设置状态了?
`与您正在使用的其他两个箭头功能一样:
componentDidMount() {
let general = {};
let values = [];
//gets selected setting form schema
getSettingsForms().then((response) => {
this.setState({settingsForms: response});
general = response[this.state.selectedSetting];
}).then(response => {
values = getSettingsConfig(this.state.selectedSetting, general);
values.then(result => {
this.setState({
formSchema: result[1],
selectedSetting: this.state.selectedSetting,
settings: result[0]
})
})
})
}
啊,我明白了,我不需要那个功能。
componentDidMount() {
let general = {};
let values = [];
//gets selected setting form schema
getSettingsForms().then((response) => {
this.setState({settingsForms: response});
general = response[this.state.selectedSetting];
}).then(response => {
values = getSettingsConfig(this.state.selectedSetting, general);
values.then(result => {
this.setState({
formSchema: result[1],
selectedSetting: this.state.selectedSetting,
settings: result[0]
})
})
})
}