【问题标题】:React does not rerender after setState used in promise in componentDidMount在 componentDidMount 中的 promise 中使用 setState 后,React 不会重新渲染
【发布时间】:2017-04-19 16:11:07
【问题描述】:

这里是代码

componentDidMount: function () {
        var self = this,
            templates;
        new Promise(function (resolve, reject) {
            API.getTemplates(function (err, result) {
                if (!err) {
                    templates = result.templates;
                    resolve();
                }
            });
        }).then(function () {
            return new Promise(function (resolve, reject) {
                _.map(templates,function (template) {
                    console.log("template.companyId " +template.companyId);
                    API.getCompanyById(template.companyId, function (err, result) {
                        if (!err) {
                            template.userId = result.company.userId;
                            resolve();
                        }
                    })
                });
            });
        }).then(function(){
            console.log("templates");
            console.log(templates);
            self.setState({templates: templates});
        });

    },

在 chrome react 工具中,状态是正确的。 在渲染方法中调用 this.state.templates。但默认为空

getInitialState: function () {
    return {templates: null}
},

看起来在 setState 执行后没有调用 rerender。此外,如果我们走另一条路线,此组件中的状态将不会保存,它将是 template:null

【问题讨论】:

  • console.log 在setState 之前触发吗?
  • 当你在log 里面then 时,你的templates 等于多少?
  • 在log里面然后显示[Object, Object, Object]@BenjaminGruenbaum idk,如何查看?
  • 为什么不将整个promise 和setState 保留在componentWillMount 中。为什么要等待组件挂载?
  • @JyothiBabuAraja 在发出请求之前我必须在页面上显示一些内容。

标签: reactjs promise setstate


【解决方案1】:

问题在于在迭代数组中做出承诺,因此如果一个请求完成,则执行该方法。 因此,我做出一系列承诺并等待每个人都成功。 这是解决方案

componentDidMount: function () {
        var self = this,
            templates;
        new Promise(function (resolve, reject) {
            API.getTemplates(function (err, result) {
                if (!err) {
                    templates = result.templates;
                    resolve();
                }
            });
        }).then(function () {
            /* for every template find his userId*/
            let copyTemplate = templates;
            let promises = [];
            for (let i = 0; i < templates.length; i++) {
                promises.push(new Promise(function (resolve, reject) {
                        API.getCompanyById(templates[i].companyId, function (err, result) {
                            if (!err) {
                                copyTemplate[i].userId = result.company.userId;
                                resolve();
                            }
                        })
                    })
                );
            }

            console.log(copyTemplate);
            templates = copyTemplate;
            return Promise.all(promises);
        }).then(function () {
            console.log("templates");
            console.log(templates);
            self.setState({templates: templates});
        });

    },

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 2019-09-26
    • 1970-01-01
    • 2019-06-18
    • 2020-02-06
    • 2021-04-28
    • 1970-01-01
    相关资源
    最近更新 更多