嗨,我实际上正在尝试使用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
}