javascript - 在 promise 中设置状态时遇到困难

标签 javascript reactjs

我的 componentDidMount() 函数中有一系列 promise 。在 values.then(function(result) promise 中,我尝试设置状态。我收到此错误 Unhandled Rejection (TypeError): Cannot read property 'setState' of undefined 但研究了这个错误,我需要使用箭头函数绑定(bind)我的 Promise,以使其能够访问 this 关键字。

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]

                })

            })
        })
    }

问题是我不知道在哪里把箭头函数放在这种 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(function(result) => {
                this.setState({
                    formSchema: result[1],
                    selectedSetting: this.state.selectedSetting,
                    settings: result[0]

                })

            })
        })
    }

将此箭头函数放入 values.then(function(result) 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]

                })

            })
        })
    }

关于javascript - 在 promise 中设置状态时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59720522/

相关文章:

javascript - 在 textarea 外部单击会丢失插入符号位置

javascript - 如果为空则不显示任何内容 (JS)

javascript - 是否可以将 javascript 值设置为 <g :set var ="x">

javascript - react : Rendering a list in reverse order

reactjs - React Material UI 表排序

javascript - 正则表达式;不匹配数字,除非后跟 '/'

javascript - 使用 Javascript 创建带有 JSON 信息的网站链接?

javascript - 从 create-react-app 中的 service worker 缓存中排除 index.html

reactjs - 使用同一库的两个版本的组件(在我的例子中是 npm/Material UI)

javascript - 空对象从无处传递到 Prop 上