简体   繁体   English

易变的-我的商店正在接收数据,但是我的组件无法与商店对话

[英]fluxible - my store is receiving data, but my component is not able to talk to the store

Basically, on loading the home page, an action is triggered that grabs data from the database in JSON form. 基本上,在加载主页时,将触发一个动作,该动作以JSON形式从数据库中获取数据。 It dispatches a SUCCESS that my store receives and updates the state of an object, posts . 它派遣一个SUCCESS,我的商店接收并更新对象的状态posts When I console.log() from the store, I see the data has indeed been received. 当我从商店访问console.log() ,我看到确实已接收到数据。 However, my component is not getting that data. 但是,我的组件没有获取该数据。

Here's my component code: 这是我的组件代码:

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;

I don't see the posts data in the props in React Developer Tools. 我没有在React Developer Tools的props中看到发布数据。

Here's the store: 这是商店:

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;

Can someone please help me out? 有人可以帮我吗?

Thanks 谢谢

It look like you are not providing context to your application. 看起来您没有在为应用程序提供上下文。 You need to instantiate your Fluxible app at the top-level React component . 您需要在顶级React组件上实例化Fluxible应用。

In order to resolve it, make a new top-level component and set it as your entry point like so: 为了解决该问题,请制作一个新的顶级组件,并将其设置为您的入口点,如下所示:

app.js app.js

import React from 'react';
import ReactDOM from 'react-dom';
import Fluxible from 'fluxible';
import { FluxibleComponent } from 'fluxible-addons-react';
import Home from './components/Home';

const app = new Fluxible();
app.registerStore(PostStore);

const context = app.createContext();

ReactDOM.render(
    <FluxibleComponent context={context.getComponentContext()}>
        <Home />
    </FluxibleComponent>,
    document.querySelector('.container')
);

Home.js Home.js

import React from 'react';
import { provideContext, connectToStores } from 'fluxible-addons-react';
import PostStore from '../stores/PostStore';
import postActions from '../actions/postActions';

class Home extends React.Component {

    dispatchAction() {
        this.context.executeAction(postActions.dispatchPost, { post: 'My Post' });
    }

    render() {
        return (
            <div onClick={this.dispatchAction.bind(this)}>
                <h2>Home</h2>
                <div></div>
            </div>
        );
    }
}

Home.contextTypes = {
    getStore: React.PropTypes.func.isRequired,
    executeAction: React.PropTypes.func.isRequired
};

Home = provideContext(Home);

Home = connectToStores(Home, [PostStore], (context) => {
    return {
        posts: context.getStore(PostStore).getPosts()
    };
});

export default Home;

postActions.js postActions.js

module.exports = {
    dispatchPost(actionContext, payload) {
        actionContext.dispatch('GET_POSTS_SUCCESS', payload);
    }
};

That should work! 那应该工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM