1

嗨,我实际上正在尝试使用flux、reactjs 和fluxible 开发一些小应用程序,并且在处理商店时遇到了问题。

实际上,我可以通过操作将信息发送到我的商店,但我不知道如何在我的组件内部的商店中接收 this.emitChange 的结果以刷新屏幕。

我应该在我的组件中添加什么来刷新我的列表?

这是我的组件:

import React from 'react';

class Client extends React.Component {

    constructor (props) {
      super(props);
      this.myListView = [];
    }

    add(e){
      this.context.executeAction(function (actionContext, payload, done) {
          actionContext.dispatch('ADD_ITEM', {name:'toto'});
      });
    }

    render() {
        return (
            <div>
                <h2>Client</h2>
                <p>List of all the clients</p>
                <button onClick={this.add.bind(this)}>Click Me</button>
                <ul>
                    {this.myListView.map(function(title) {
                      return <li key={name}>{name}</li>;
                    })}
                </ul>
            </div>
        );
    }
}


Client.contextTypes = {
    executeAction: React.PropTypes.func.isRequired
};

export default Client;

这是我的商店

import BaseStore from 'fluxible/addons/BaseStore';

class ListStore extends BaseStore {

  constructor(dispatcher) {
      super(dispatcher);
      this.listOfClient = [];
    }

  dehydrate() {
      return {
          listOfClient: this.listOfClient
      };
  }

  rehydrate(state) {
      this.listOfClient = state.listOfClient;
  }


  addItem(item){
    this.listOfClient.push(item);
    this.emitChange();
  }

}

ListStore.storeName = 'ListStore';
ListStore.handlers = {
    'ADD_ITEM': 'addItem'
};

export default ListStore;

更新

this.setState 应用不好

_onStoreChange() {
      console.log(this.getStoreState()) // gives me the good list
      this.setState(this.getStoreState()); // doesn't update the list, this.myListView gives [] always
    }
4

1 回答 1

1

您可能想要放入myListView组件的状态并在实例化时从存储中填充它。

所以你的组件最终会是这样的:

import ListStore from '../stores/ListStore';
class MyComponent extends React.Component {
    static contextTypes = {
        getStore: React.PropTypes.func.isRequired,
        executeAction: React.PropTypes.func.isRequired
    }

    constructor(props) {
        super(props);
        this.state = this.getStoreState();
        this.boundChangeListener = this._onStoreChange.bind(this);
    }
    getStoreState () {
        return {
            myListView: this.context.getStore(ListStore).getItems()
        }
    }
    componentDidMount () {
        this.context.getStore(ListStore).addChangeListener(this.boundChangeListener);
    }
    componentWillUnmount () {
        this.context.getStore(ListStore).removeChangeListener(this.boundChangeListener);
    }
    _onStoreChange () {
        this.setState(this.getStoreState());
    }
    add(e){
      this.context.executeAction(function (actionContext, payload, done) {
          actionContext.dispatch('ADD_ITEM', {name:'toto'});
       });
    }
    render () {
    return (
        <div>
            <h2>Client</h2>
            <p>List of all the clients</p>
            <button onClick={this.add.bind(this)}>Click Me</button>
            <ul>
                {this.state.myListView.map(function(title) {
                  return <li key={name}>{name}</li>;
                })}
            </ul>
        </div>
    );
    }
}

这样,您将听到更改并触发setState组件,从而导致重新渲染。

更新add方法

在上面的原始代码中,我不确定单击时执行操作的方式是否正确。也许尝试:

add(e) {
    this.context.executeAction(function(actionContext, payload, done) {
        actionContext.dispatch('ADD_ITEM', payload);
        done();
    }, {name: 'toto'});
}
于 2015-09-03T13:42:34.157 回答