admin管理员组文章数量:1279008
I am new to ReactJS. When i am reading ReactJS Blueprint book author had specified that
"but take care to never run setState in here, as that will trigger an endless update loop."
So, i have created a below simple ponent but i did not see any such infinite loop happening.
'use strict';
import React from 'react';
import { render } from 'react-dom';
const App = React.createClass({
displayName : "SG",
getDefaultProps() {
return {
age : "24"
}
},
getInitialState() {
return {
date : ""
}
},
ponentDidMount() {
debugger;
var d = new Date();
this.setState({date: d.getMilliseconds().toString()});
},
render() {
return (
<section>
<h1>Demo App</h1>
<p>Name : {this.props.name}</p>
<p>Age : {this.props.age}</p>
<p>date : {this.state.date}</p>
</section>
);
}
});
render (<App name='Gowtham S'/>, document.getElementById('container'));
I kept debugger in ponentDidMount but it is hitting only once. Whats wrong with my code can anyone help me?
Thanks
I am new to ReactJS. When i am reading ReactJS Blueprint book author had specified that
"but take care to never run setState in here, as that will trigger an endless update loop."
So, i have created a below simple ponent but i did not see any such infinite loop happening.
'use strict';
import React from 'react';
import { render } from 'react-dom';
const App = React.createClass({
displayName : "SG",
getDefaultProps() {
return {
age : "24"
}
},
getInitialState() {
return {
date : ""
}
},
ponentDidMount() {
debugger;
var d = new Date();
this.setState({date: d.getMilliseconds().toString()});
},
render() {
return (
<section>
<h1>Demo App</h1>
<p>Name : {this.props.name}</p>
<p>Age : {this.props.age}</p>
<p>date : {this.state.date}</p>
</section>
);
}
});
render (<App name='Gowtham S'/>, document.getElementById('container'));
I kept debugger in ponentDidMount but it is hitting only once. Whats wrong with my code can anyone help me?
Thanks
Share Improve this question asked Mar 29, 2017 at 13:36 GowthamGowtham 3954 silver badges13 bronze badges1 Answer
Reset to default 14He is talking about ponentDidUpdate, not ponentDidMount.
When you setState
in ponentDidUpdate
, the latter is being called again because the ponent should update, which results in endless recursion. On the other hand, ponentDidMount is only called on the initial render.
本文标签: javascriptReactsetState in componentDidMount not causing infinite loopStack Overflow
版权声明:本文标题:javascript - React : setState in componentDidMount not causing infinite loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741246614a2364997.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论