我有错误maximum call stack size exceeded
。也许我以错误的方式理解componentDidUpdate
,但它不应该运行一次,而不是1000.如何解决它?
class App extends Component {
constructor(props) {
super(props);
this.state = {
amount: 0
}
}
updateAmout() {
let number = 0;
this.props.comments.map((comment, index) => {
if (comment.replyTo === null) {
number += 1;
this.setState({amount: number});
}
return null;
});
}
componentWillMount() {
this.updateAmout();
}
componentDidUpdate() {
this.updateAmout();
}
render() {
console.log(this.state.amount);
return (
<div className="comments-container">
<div id="comments">
<AddComment />
<div className="comments-flow">
<div className="comments-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