admin管理员组文章数量:1305615
In this example, when I try to update the state during the ponentDidUpdate
life cycle callback, I get a too much recursion
error. How should I be updating the state?
import React from 'react';
class NotesContainer extends React.Component {
constructor(props) {
super(props);
this.state = { listOfShoppingItems: [] };
}
ponentDidUpdate(nextProps, nextState) {
let newShoppingItems = this.calculateShoppingItems();
this.setState({ listOfShoppingItems: newShoppingItems });
}
calculateShoppingItems() {
let shoppingItemsCart = []
if (this.props.milk < 3) {
let value = "Buy some milk";
shoppingItemsCart.push(value);
}
if (this.props.bread < 2) {
let value = "Buy some bread";
shoppingItemsCart.push(value);
}
if (this.props.fruit < 10) {
let value = "Buy some fruit";
shoppingItemsCart.push(value);
}
if (this.props.juice < 2) {
let value = "Buy some juice";
shoppingItemsCart.push(value);
}
if (this.props.sweets < 5) {
let value = "Buy some sweets";
shoppingItemsCart.push(value);
}
return shoppingItemsCart;
}
render() {
return (
<div>
Etc...
</div>
);
}
}
export default NotesContainer;
In this example, when I try to update the state during the ponentDidUpdate
life cycle callback, I get a too much recursion
error. How should I be updating the state?
import React from 'react';
class NotesContainer extends React.Component {
constructor(props) {
super(props);
this.state = { listOfShoppingItems: [] };
}
ponentDidUpdate(nextProps, nextState) {
let newShoppingItems = this.calculateShoppingItems();
this.setState({ listOfShoppingItems: newShoppingItems });
}
calculateShoppingItems() {
let shoppingItemsCart = []
if (this.props.milk < 3) {
let value = "Buy some milk";
shoppingItemsCart.push(value);
}
if (this.props.bread < 2) {
let value = "Buy some bread";
shoppingItemsCart.push(value);
}
if (this.props.fruit < 10) {
let value = "Buy some fruit";
shoppingItemsCart.push(value);
}
if (this.props.juice < 2) {
let value = "Buy some juice";
shoppingItemsCart.push(value);
}
if (this.props.sweets < 5) {
let value = "Buy some sweets";
shoppingItemsCart.push(value);
}
return shoppingItemsCart;
}
render() {
return (
<div>
Etc...
</div>
);
}
}
export default NotesContainer;
Share
Improve this question
edited Aug 16, 2017 at 15:45
Mike Walton
4,3863 gold badges44 silver badges54 bronze badges
asked Aug 25, 2015 at 7:55
SimpletonSimpleton
6,41512 gold badges55 silver badges89 bronze badges
1 Answer
Reset to default 9ponentDidUpdate
is triggered when either the props or the state has changed. If you change the state in this method, you are causing an infinite loop (unless you implement shouldComponentUpdate
).
It looks like your state changes when you receive new props, therefore ponentWillReceiveProps
seems a good place. From the docs:
Invoked when a ponent is receiving new props. This method is not called for the initial render.
Use this as an opportunity to react to a prop transition before
render()
is called by updating the state usingthis.setState()
. The old props can be accessed via this.props. Callingthis.setState()
within this function will not trigger an additional render.
本文标签: javascriptToo much recursion when updating state in reactStack Overflow
版权声明:本文标题:javascript - Too much recursion when updating state in react - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803433a2398360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论