ReactJS - ComponentDidMount is executing before render

微笑、不失礼 提交于 2019-12-24 15:42:51

问题


I´m having some problems with react. I´m using map function at render and the function componentDidMount is being called before it finish.

Here snippet of my code

componentDidMount: function() {
   console.info("didMount");
},
render: function() {
   return React.createElement("div", null, 
             this.state.fields.map(function(field) {
                console.info("field" + field);
                return React.createElement("span", null, field);
             }.bind(this)));
}

It being printed "didMount" before "field...". How can i solve this? It seems that map is async.

Thank´s


回答1:


How are you initializing your this.state.fields? If you do it in the componentDidMount function, then console.info("field" + field); never gets executed because the field/array is null.

Use the setState callback parameter to run code after the state has been initialized/updated.



来源:https://stackoverflow.com/questions/35176172/reactjs-componentdidmount-is-executing-before-render

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!