/**
* Created by yanggang on 2017/2/23.
*/
import React from 'react';
import Reflux from 'reflux';
var CounterActions = Reflux.createActions([
'add'
]);
class CounterStore extends Reflux.Store {
constructor(props) {
super(props);
this.state = {num:0};
this.num = 0;
this.listenables = CounterActions;
}
onAdd(num) {
this.state.num = this.state.num + (num==undefined?1:num);
this.trigger(this.state,2);
//or
//this.setState({num:(num==undefined?1:num)});
}
}
var counterStore = new CounterStore();
class Counter extends Reflux.Component {
constructor(props) {
super(props);
this.store = counterStore;
this.unsubscribe = counterStore.listen(this.onStatusChange);
}
onStatusChange(state,status) {
console.log(status);
}
componentWillUnmount() {
this.unsubscribe();
}
render() {
return <div>{this.state.num}</div>;
}
}
export default Counter;
exports.CounterActions = CounterActions;
exports.CounterStore = CounterStore;
网友评论