美文网首页
reflux的正确姿势

reflux的正确姿势

作者: yang_21312 | 来源:发表于2017-02-24 00:16 被阅读0次
/**
 * 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;

相关文章

网友评论

      本文标题:reflux的正确姿势

      本文链接:https://www.haomeiwen.com/subject/xfeiwttx.html