Yon*_*irt 6 javascript reactjs react-router refluxjs
我面临一个无限循环问题,我无法看到触发它的是什么.它似乎在渲染组件时发生.
我有三个组件,组织如下:
TimelineComponent
|--PostComponent
|--UserPopover
Run Code Online (Sandbox Code Playgroud)
TimelineComponenet:
React.createClass({
mixins: [
Reflux.listenTo(TimelineStore, 'onChange'),
],
getInitialState: function() {
return {
posts: [],
}
},
componentWillMount: function(){
Actions.getPostsTimeline();
},
render: function(){
return (
<div className="timeline">
{this.renderPosts()}
</div>
);
},
renderPosts: function (){
return this.state.posts.map(function(post){
return (
<PostComponenet key={post.id} post={post} />
);
});
},
onChange: function(event, posts) {
this.setState({posts: posts});
}
});
Run Code Online (Sandbox Code Playgroud)
PostComponent:
React.createClass({
...
render: function() {
return (
...
<UserPopover userId= {this.props.post.user_id}/>
...
);
}
});
Run Code Online (Sandbox Code Playgroud)
UserPopover:
module.exports = React.createClass({
mixins: [
Reflux.listenTo(UsersStore, 'onChange'),
],
getInitialState: function() {
return {
user: null
};
},
componentWillMount: function(){
Actions.getUser(this.props.userId);
},
render: function() {
return (this.state.user? this.renderContent() : null);
},
renderContent: function(){
console.log(i++);
return (
<div>
<img src={this.state.user.thumbnail} />
<span>{this.state.user.name}</span>
<span>{this.state.user.last_name}</span>
...
</div>
);
},
onChange: function() {
this.setState({
user: UsersStore.findUser(this.props.userId)
});
}
});
Run Code Online (Sandbox Code Playgroud)
最后,还有UsersStore**:
module.exports = Reflux.createStore({
listenables: [Actions],
users: [],
getUser: function(userId){
return Api.get(url/userId)
.then(function(json){
this.users.push(json);
this.triggerChange();
}.bind(this));
},
findUser: function(userId) {
var user = _.findWhere(this.users, {'id': userId});
if(user){
return user;
}else{
this.getUser(userId);
return [];
}
},
triggerChange: function() {
this.trigger('change', this.users);
}
});
Run Code Online (Sandbox Code Playgroud)
除UserPopover组件外,一切正常.
对于每个PostComponent,渲染一个UserPopOver,用于获取willMount循环中的数据.
问题是,如果您注意到我console.log(i++);在UserPopover组件中有这行代码,则会反复递增
...
3820
3821
3822
3823
3824
3825
...
Run Code Online (Sandbox Code Playgroud)
清除一个无限循环,但我真的不知道它来自哪里.如果有人能给我一个提示,我将非常感激.
PS:我已经在UsersStore中尝试过这种方法,但是所有的PostComponent都有相同的"用户":
...
getUser: function(userId){
return Api.get(url/userId)
.then(function(json){
this.user = json;
this.triggerChange();
}.bind(this));
},
triggerChange: function() {
this.trigger('change', this.user);
}
...
Run Code Online (Sandbox Code Playgroud)
并在UserPopover中
...
onChange: function(event, user) {
this.setState({
user: user
});
}
...
Run Code Online (Sandbox Code Playgroud)
因为您的帖子是异步获取的,所以我相信当您的 UserPopover 组件执行它的 componentWillMount 时, props.userId 未定义,然后您调用 UsersStore.findUser(this.props.userId) ,在 UserStore 中,调用 getUser 因为它在本地存储中找不到用户。
请注意,每次 getUser 的 ajax 完成时,它都会触发。因此UserPopover组件执行onChange函数,并再次调用UsersStore.findUser。这是一个无限循环。
请在 UserPopover 的 componentWillMount 中添加一个 console.log(this.props.userId) 来查看它是否像我上面所说的那样。我实际上并不是100%确定。
这是一个问题,所有 UserPopover 实例共享相同的 UserStore,我认为我们应该重新考虑这些组件和存储的结构。但我还没有想出最好的办法。
| 归档时间: |
|
| 查看次数: |
4613 次 |
| 最近记录: |