gpt4 book ai didi

javascript - ComponentWillMount 触发不停止?

转载 作者:行者123 更新时间:2023-12-02 15:15:42 24 4
gpt4 key购买 nike

首先,我是 React 新手,这是我的第一个项目。

我正在创建一个基本的博客应用程序,我的应用程序类如下所示:

var App = React.createClass({

getInitialState: function () {
return {
posts: Store.getPosts(),
newPost: {
headerText: '',
bodyText: ''
}
};
},

componentWillMount: function () {
console.info("App component will mount")
Store.addChangeListener(this.changeState);
},

componentWillUnmount: function () {
Store.removeChangeListener(this.changeState);
},

componentDidMount: function() {
console.info("App component mounted.")
Store.removeChangeListener(this.changeState);
} ,

changeState: function () {
this.setState({
posts: Store.getPosts()
});
},

当我的应用程序运行 componentWillMount 方法时,触发并调用 this.changeState 方法,并从通量存储中调用 Store.getPosts() 方法。

但这件事并没有停止。不断地从服务器调用xhr请求。应用程序启动时我的网络控制台如下所示。

enter image description here

我尝试添加 componentDidMount 来解决此问题,但在这种情况下,应用程序启动时不会从服务器收到帖子。

我只想在应用程序启动时加载所有帖子一次,然后在调用 setState 方法时运行。

最佳答案

可疑的是changeState函数。您使用什么通量实现?通常应该看起来像:

   changeState: function (state) {
this.setState(state);
}

通常,您与存储进行交互,抛出操作并监听存储更改,而无需直接调用任何方法。

关于javascript - ComponentWillMount 触发不停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34493101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com