reflux的正确姿势

2017-02-24  本文已影响0人  yang_21312
/**
 * 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;
上一篇 下一篇

猜你喜欢

热点阅读