简体   繁体   English

简单的 React 组件抛出“递归过多”错误

[英]Simple React component throwing "too much recursion" error

I came across this barebones React component in a recent question.我在最近的一个问题中遇到了这个准系统 React 组件。 I was playing around with it, and tried to add a new item (an object) to the array of existing objects in state .我在玩弄它,并尝试将一个新项目(一个对象)添加到state中现有对象的数组中。 But it throws "too much recursion" error.但它会引发“递归过多”错误。

I've looked at other questions with similar problems in React, but I'm not invoking this.setState() in componentDidMount() .我在 React 中查看了其他有类似问题的问题,但我没有在componentDidMount()调用this.setState() componentDidMount() Neither am I rendering the same component from it's render() method.我也没有从它的render()方法渲染相同的组件。 So, I'm not sure what causes this error.所以,我不确定是什么导致了这个错误。

 class App extends React.Component { constructor(props) { super(props); this.state = { Items: [ {w:1, b:8, name: 'banana'}, {w:7, b:3, name:'apple'}, {w:3, b:5, name:'kiwi'}, {w:6, b:3, name:'strawberry'}, {w:5, b:1, name:'orange'}] }; this.addItem = this.addItem.bind(this); //this.handleAdd = this.handleAdd.bind(this); } // handleAdd() { // this.addItem(10, 5, "mangoes"); // } addItem(){ this.setState(prevState => { Items: [...prevState.Items, {w: 20, b: 3, name: "mangoes"}]; }); } render() { return ( <div> <button className="btn btn-default" onClick={this.addItem}>Add</button> <ul> {this.state.Items.map(e => <li key={e.name}>{`${e.name} ${ew}`}</li>)} </ul> </div> ) } } ReactDOM.render(<App/>, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"> </div>

The initial set of items that was set in the constructor does render at the start, but the "add" button doesn't seem to be working.在构造函数中设置的初始项目集在开始时确实呈现,但“添加”按钮似乎不起作用。

Additionally, along with the recursion error, I came across "RegExp too big" a few times, while I was testing this.此外,随着递归错误,我在测试时遇到了几次“RegExp too big”。

Your syntax is incorrect.你的语法不正确。 When using setState with prevState syntax, you need to return the updated state and also you shouldn't have a terminating ;使用带有prevState语法的setState时,您需要返回更新后的状态,并且不应有终止; inside the updater function.在更新程序函数中。

 class App extends React.Component { constructor(props) { super(props); this.state = { Items: [ {w:1, b:8, name: 'banana'}, {w:7, b:3, name:'apple'}, {w:3, b:5, name:'kiwi'}, {w:6, b:3, name:'strawberry'}, {w:5, b:1, name:'orange'}] }; this.addItem = this.addItem.bind(this); //this.handleAdd = this.handleAdd.bind(this); } // handleAdd() { // this.addItem(10, 5, "mangoes"); // } addItem(){ this.setState(prevState => ({ // notice here use return or use wrapping () Items: [...prevState.Items, {w: 20, b: 3, name: "mangoes"}] // <-- not semicolon here })); } render() { return ( <div> <button className="btn btn-default" onClick={this.addItem}>Add</button> <ul> {this.state.Items.map(e => <li key={e.name}>{`${e.name} ${ew}`}</li>)} </ul> </div> ) } } ReactDOM.render(<App/>, document.getElementById('root'));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM