admin管理员组文章数量:1303551
I have error maximum call stack size exceeded
. Maybe I in wrong way understand ponentDidUpdate
, but shouldn't it's run one time, instead 1000. How fix it?
class App extends Component {
constructor(props) {
super(props);
this.state = {
amount: 0
}
}
updateAmout() {
let number = 0;
this.propsments.map((ment, index) => {
if (ment.replyTo === null) {
number += 1;
this.setState({amount: number});
}
return null;
});
}
ponentWillMount() {
this.updateAmout();
}
ponentDidUpdate() {
this.updateAmout();
}
render() {
console.log(this.state.amount);
return (
<div className="ments-container">
<div id="ments">
<AddComment />
<div className="ments-flow">
<div className="ments-header">
<div className="pull-right">
<a href="" className="text-muted">Best</a> |
<a href="" className="active">Newest</a> |
<a href="" className="text-muted">Oldest</a>
</div>
<b>6 Comments</b>
</div>
<RenderComments />
</div>
<button className="btn btn-block">More...</button>
</div>
</div>
) // return
} // render
} // App
I have error maximum call stack size exceeded
. Maybe I in wrong way understand ponentDidUpdate
, but shouldn't it's run one time, instead 1000. How fix it?
class App extends Component {
constructor(props) {
super(props);
this.state = {
amount: 0
}
}
updateAmout() {
let number = 0;
this.props.ments.map((ment, index) => {
if (ment.replyTo === null) {
number += 1;
this.setState({amount: number});
}
return null;
});
}
ponentWillMount() {
this.updateAmout();
}
ponentDidUpdate() {
this.updateAmout();
}
render() {
console.log(this.state.amount);
return (
<div className="ments-container">
<div id="ments">
<AddComment />
<div className="ments-flow">
<div className="ments-header">
<div className="pull-right">
<a href="" className="text-muted">Best</a> |
<a href="" className="active">Newest</a> |
<a href="" className="text-muted">Oldest</a>
</div>
<b>6 Comments</b>
</div>
<RenderComments />
</div>
<button className="btn btn-block">More...</button>
</div>
</div>
) // return
} // render
} // App
Share
Improve this question
edited Jul 10, 2017 at 22:40
Rami Chasygov
asked Jul 10, 2017 at 22:10
Rami ChasygovRami Chasygov
2,78410 gold badges26 silver badges39 bronze badges
2
-
1
ponentWillMount
will force an update which will triggerponentDidUpdate
which will force again an update and so on. – MinusFour Commented Jul 10, 2017 at 22:17 -
1
ponentDidUpdate()
is a callback to notify you that the state update was finished successfully. You should not trigger another state update here. – Code-Apprentice Commented Jul 10, 2017 at 22:45
2 Answers
Reset to default 6Each time you modify state in ponentDidUpdate
, a re-render is thrown asynchronously.
In your method updateAmount
, you are modifying the state.
And you are calling that method in ponentDidUpdate
, so you initiate an infinite loop of re-renders, so this endless loop created finally wastes the javascript memory.
The React cycle when updating the state is the following one. So you can easily understand why you enter into an endless loop.
I found the answer to my question. I need to use the nextProps
argument and the method ponentWillUpdate
instead of the ponentDidUpdate
method.
// before
ponentDidUpdate() {
this.updateAmout();
}
// after
ponentWillUpdate(nextProps) {
if (!_.isEqual(this.props, nextProps)) {
this.updateAmout();
}
}
本文标签: javascriptMaximum call stack size exceeded ReactReduxStack Overflow
版权声明:本文标题:javascript - Maximum call stack size exceeded React-Redux - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741750916a2395823.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论