基本上,在加载主页时,会触发一个以 JSON 形式从数据库中获取数据的操作。它分派我的商店接收到的 SUCCESS 并更新对象的状态posts
。当我console.log()
从商店出来时,我看到数据确实已经收到。但是,我的组件没有获取该数据。
这是我的组件代码:
import React from 'react';
import connectToStores from 'fluxible-addons-react/connectToStores';
import PostStore from '../stores/PostStore';
class Home extends React.Component {
render() {
return (
<div>
<h2>Home</h2>
<div></div>
</div>
);
}
}
Home = connectToStores(Home, [PostStore], (context, props) => ({
posts : context.getStore(PostStore).getPosts()
}))
export default Home;
我在 React Developer Tools 的 props 中看不到帖子数据。
这里是商店:
import BaseStore from 'fluxible/addons/BaseStore';
class PostStore extends BaseStore {
constructor(dispatcher) {
super(dispatcher);
this.posts = null;
}
handleGetPostsSuccess(payload) {
this.posts = payload;
console.log("from PostStore",this.posts);
this.emitChange();
}
getPosts() {
return this.posts;
}
//send state to client
dehydrate() {
return {
posts : this.posts
}
}
//server state
rehydrate(state) {
this.posts = state.posts;
}
}
PostStore.storeName = 'PostStore';
PostStore.handlers = {
'GET_POSTS_SUCCESS' : 'handleGetPostsSuccess'
};
export default PostStore;
有人可以帮我吗?
谢谢