实操《深入浅出React和Redux》第二期—Flux

此书讲得蛮详细,

从Flux一步一步过渡到Redux

写过的代码舍不得扔,

立此存照吧。

我有几张阿里云幸运券分享给你,用券购买或者升级阿里云相应产品会有特惠惊喜哦!把想要买的产品的幸运券都领走吧!快下手,马上就要抢光了。

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';

import ControlPanel from './views/ControlPanel';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<ControlPanel />,document.getElementById('root'));
registerServiceWorker();


Appdispatcher.js

import {dispatcher} from 'flux';

export default new dispatcher();


ActionTypes.js

export const INCREMENT = 'increment';
export const DECREMENT = 'decrement';


Actions.js

import * as ActionTypes from './ActionTypes';
import Appdispatcher from './Appdispatcher';

export const increment = (counterCaption) => {
	Appdispatcher.dispatch({
		type: ActionTypes.INCREMENT,counterCaption: counterCaption
	});
};


export const decrement = (counterCaption) => {
	Appdispatcher.dispatch({
		type: ActionTypes.DECREMENT,counterCaption: counterCaption
	});
};


CounterStore.js

import Appdispatcher from '../Appdispatcher';
import * as ActionTypes from '../ActionTypes';
import {EventEmitter} from 'events';

const CHANGE_EVENT = 'changed';

const counterValues = {
	'First': 0,'Second': 10,'Third': 30
};

const CounterStore = Object.assign({},EventEmitter.prototype,{
	getCounterValues: function() {
		return counterValues;
	},emitChange: function() {
		this.emit(CHANGE_EVENT);
	},addchangelistener:function(callback) {
		this.on(CHANGE_EVENT,callback);
	},removechangelistener: function(callback) {
		this.removeListener(CHANGE_EVENT,callback);
	}
});

CounterStore.dispatchToken = Appdispatcher.register((action)=> {
	if (action.type === ActionTypes.INCREMENT) {
		counterValues[action.counterCaption]++;
		CounterStore.emitChange();
	} else if (action.type === ActionTypes.DECREMENT) {
		counterValues[action.counterCaption]--;
		CounterStore.emitChange();
	}
});

export default CounterStore;


SummaryStore.js

import Appdispatcher from '../Appdispatcher';
import * as ActionTypes from '../ActionTypes';
import CounterStore from './CounterStore';
import {EventEmitter} from 'events';

const CHANGE_EVENT = 'changed';

function computerSummary(counterValues) {
	let summary = 0;
	for (const key in counterValues) {
		if (counterValues.hasOwnProperty(key)) {
			summary += counterValues[key];
		}
	}
	return summary;
}

const SummaryStore = Object.assign({},{
	getSummary: function() {
		return computerSummary(CounterStore.getCounterValues());
	},addchangelistener: function(callback) {
		this.on(CHANGE_EVENT,callback);
	}
});

SummaryStore.dispatchToken = Appdispatcher.register((action) => {
	if ((action.type === ActionTypes.INCREMENT) ||
		(action.type === ActionTypes.DECREMENT)) {
			Appdispatcher.waitFor([CounterStore.dispatchToken]);
			SummaryStore.emitChange();
		}
});

export default SummaryStore;


CounterPanel.js

import React,{ Component } from 'react';
import Counter from './Counter';
import Summary from './Summary';

const style = {
	margin: '20px'
};

class ControlPanel extends Component {
	render() {
		return (
			<div style={style}>
				<Counter caption="First" />
				<Counter caption="Second" />
				<Counter caption="Third"  />
				<hr />
				<Summary />
			</div>
		);
	}
}

export default ControlPanel;


Counter.js

import React,{ Component } from 'react';
import PropTypes from 'prop-types';

import * as Actions from '../Actions.js';
import CounterStore from '../stores/CounterStore.js';

const buttonStyle = {
	margin: '10px'
};

const propTypes  = {
	caption: PropTypes.string.isrequired
};

class Counter extends Component {
	constructor(props) {
		super(props);
		
		this.onClickIncrementButton = this.onClickIncrementButton.bind(this);
		this.onClickDecrementButton = this.onClickDecrementButton.bind(this);
		this.onChange = this.onChange.bind(this);
		
		this.state = {
			count: CounterStore.getCounterValues()[props.caption]
		}
	}
	
	shouldComponentUpdate(nextProps,nextState) {
		return (nextProps.caption !== this.props.caption) ||
				(nextState !== this.state.count);
	}
	
	componentDidMount() {
		CounterStore.addchangelistener(this.onChange);
	}
	
	componentwillUnmout() {
		CounterStore.removechangelistener(this.onChange);
	}
	
	onChange() {
		const newCount = CounterStore.getCounterValues()[this.props.caption];
		this.setState({count: newCount});
	}
	
	onClickIncrementButton() {
		Actions.increment(this.props.caption);
	}
	
	onClickDecrementButton() {
		Actions.decrement(this.props.caption);
	}
	
	
	
	render() {

		const {caption} = this.props;
		return (
			<div>
				<button style={buttonStyle} onClick={this.onClickIncrementButton}>+</button>
				<button style={buttonStyle} onClick={this.onClickDecrementButton}>-</button>
				<span> { caption }  count: {this.state.count}</span>
			</div>
		);
	}
}

Counter.defaultProps = {
	initValue: 0,onUpdate: f => f
};
Counter.propTypes = propTypes

export default Counter;


Summary.js

import React,{Component} from 'react';
import SummaryStore from '../stores/SummaryStore';

class Summary extends Component {
	constructor(props) {
		super(props);
		this.onUpdate = this.onUpdate.bind(this);
		this.state = {
			sum: SummaryStore.getSummary()
		}
	}
	
	componentDidMount() {
		SummaryStore.addchangelistener(this.onUpdate);
	}
	
	componentwillUnmount() {
		SummaryStore.removechangelistener(this.onUpdate);
	}
	
	onUpdate() {
		this.setState({
			sum: SummaryStore.getSummary()
		})
	}
	
	render() {
		return (
			<div>Total Count: {this.state.sum}</div>
		);
	}
}

export default Summary;
阅读原文

http://click.aliyun.com/m/36077/

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...