reflux的正确姿势

/**
 * 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 
{this.state.num}
; } } export default Counter; exports.CounterActions = CounterActions; exports.CounterStore = CounterStore;

你可能感兴趣的:(reflux的正确姿势)