ReactJS 中的最大调用堆栈超出错误。有人可以帮助解释发生了什么吗?(JSFiddle 上的片段)

IT技术 reactjs
2021-04-29 13:31:09

我是 ReactJS 的新手,正在尝试一个简单的项目。基本上,该代码段从数组中创建一个朋友列表并显示朋友的总数。

出于某种原因,当我添加一个新朋友时,我意识到 incrementFriendsCount 函数会抛出“最大调用堆栈超出错误”

下面的代码片段也可以在 JSFiddle 上找到

var HelloUser = React.createClass({
  getInitialState: function() {
    return {
      name: "Toyosi",
      friends: ["Susanna", "Jibola", "Worreva"],
      friendsCount: 0
    }
  },
  addFriends: function(friend) {
    this.setState({
      friends: this.state.friends.concat([friend])
    });
  },
  componentWillMount: function() {
    this.setState({
      friendsCount: this.state.friends.length
    });
  },
  incrementFriendsCount: function() {
    this.setState({
      friendsCount: this.state.friends.length
    });
  },
  render: function() {
    return ( < div >
      Villain: {
        this.state.name
      }, No of friends: {
        this.state.friendsCount
      } < br / >
      < AddingTheFriend addNew = {
        this.addFriends
      }
      incCount = {
        this.incrementFriendsCount
      }
      />
        <ListFriends enemies={this.state.friends} / >
      < /div>
    );
  }
});

var ListFriends = React.createClass({
    propTypes: {
    	enemies: React.PropTypes.array.isRequired
    },
    render: function() {
    	var allFriends = this.props.enemies.map(function(friend){
        	return <li>{friend}</li > ;
    });

  return ( < div > Her evil friends:
    < ul > {
      allFriends
    } < /ul>
            </div >
  )
}
});

var AddingTheFriend = React.createClass({
      getInitialState: function() {
        return {
          newFriend: ''
        }
      },
      propTypes: {
        addNew: React.PropTypes.func.isRequired
      },
      updateNewFriend: function(change) {
        this.setState({
          newFriend: change.target.value
        });
      },
      addTheFriend: function() {
        this.props.addNew(this.state.newFriend);
        this.setState({
          newFriend: ''
        })
      },
      componentWillReceiveProps: function() {
        this.props.incCount();
      },
      render: function() {
          return ( < div >
            < input type = "text"
            value = {
              this.state.newFriend
            }
            onChange = {
              this.updateNewFriend
            }
            />
                <button type="button" onClick={this.addTheFriend}>Add Friend</button >
            < /div>
        )
    }
});
React.render(<HelloUser / > , document.getElementById('app'));
<script src="http://fb.me/react-js-fiddle-integration.js"></script>

<div id="app"></div>

如果有人能更多地了解为什么会抛出这个错误,我将不胜感激。

3个回答

您正在呼吁this.props.incCountcomponentWillReceiveProps其中设置父组件和影响的国家将是AddingTheFriend再次呈现,并this.props.incCount再次调用。因此堆栈溢出。

另一个建议是,通常您要小心并setState尽可能少地使用,尽可能少的组件。在将新朋友连接到父组件的状态的同时简单地增加朋友计数。

这是代码笔——在我看来比 JSFiddle 好得多。

作为使用 react 时出现相同错误输出的人的未来参考:如果您同时运行您的应用程序,也会发生此错误

  • webpack-dev-server --hot
  • 新的 webpack.HotModuleReplacementPlugin()

所以:当您使用 --hot 运行您的服务器并在您的 webpack 配置中启用 hotModuleReplacementPlugin 时,您将进入组件将递归更新的情况,从而准确地生成 OP 提到的错误消息。

简单的解决方案:省略两件事之一,例如省略“--hot”,因为您已经为此使用了插件。

就我而言,这是一个无限循环,或者如果您喜欢明显的逻辑错误。