gpt4 book ai didi

javascript - React Js,Reflux 在 ajax 响应之前渲染

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:58:33 27 4
gpt4 key购买 nike

我想通过 api 调用获取我的用户列表,并呈现一个包含数据的表格。

目前我可以获取数据,但是当我尝试显示它时出现错误。

我认为 React 在 api 调用结束之前渲染不明白为什么。

这是我的代码:

var Actions = Reflux.createActions([
"fetchList"
]);

这是我的商店:

var bannersStore  = Reflux.createStore({
users: { data : {}},
listenables: [Actions],

init: function() {

this.fetchList();

},
fetchList: function(){

var self = this;

reqwest({
url: 'http://localhost:9080/api/member.json',
method: 'get',
success: function (resp) {
console.log('fetch complete');
self.users = resp;
self.trigger({users :resp});

}
});
}

});

这是我的 React 类:

var Users = React.createClass({

getInitialState: function() {
return {users : UsersStore.fetchList()};
},

render: function() {

var usersRows = this.state.users.data.map(function(user, i) {

return (
<tr key={i}>
<td><Link to="user" params={{ id: user.id }}>{user.attributes.firstname + ' ' + user.attributes.lastname}</Link></td>
<td>{user.attributes.email}</td>
<td>{user.status}</td>
<td>{user.language}</td>
</tr>
)
});

return (
<div>
<table className="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Image</th>
<th>URL</th>
<th>Active?</th>
</tr>
</thead>
<tbody>
{ usersRows }
</tbody>
</table>
</div>
)

}

});

this.state.users.data 是未定义的,我有一个错误(未定义)。

感谢您的帮助。

最佳答案

我建议使用这种模式。可以在 https://github.com/calitek/ReactPatterns 找到回流模式的示例。 React.14/ReFluxSuperAgent.

    getInitialState: function() {
return {users : {data: []}};
},
componentDidMount() {
this.unsubscribe = UsersStore.listen(this.storeDidChange);
Actions.fetchList();
},
componentWillUnmount() { this.unsubscribe(); },
storeDidChange(newData) {
this.setState(newData);
},

关于javascript - React Js,Reflux 在 ajax 响应之前渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33831938/

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