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

简介: 此书讲得蛮详细, 从Flux一步一步过渡到Redux。 写过的代码舍不得扔, 立此存照吧。
此书讲得蛮详细,
从Flux一步一步过渡到Redux。
写过的代码舍不得扔,
立此存照吧。

index.js

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({}, EventEmitter.prototype, {
	getSummary: function() {
		return computerSummary(CounterStore.getCounterValues());
	},
	
	emitChange: function() {
		this.emit(CHANGE_EVENT);
	},
	
	addChangeListener: function(callback) {
		this.on(CHANGE_EVENT, callback);
	},
	
	removeChangeListener: function(callback) {
		this.removeListener(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;



目录
相关文章
|
1月前
|
存储 JavaScript 前端开发
掌握现代Web开发的基石:深入理解React与Redux
【10月更文挑战第14天】掌握现代Web开发的基石:深入理解React与Redux
33 0
|
5天前
|
前端开发 JavaScript 开发者
使用React和Redux构建高效的前端应用
使用React和Redux构建高效的前端应用
15 1
|
1月前
|
存储 JavaScript 前端开发
React中使用redux
【10月更文挑战第15天】
31 3
|
1月前
|
存储 JavaScript 前端开发
如何使用React和Redux构建现代化Web应用程序
【10月更文挑战第4天】如何使用React和Redux构建现代化Web应用程序
|
1月前
|
JavaScript 前端开发
使用 React 和 Redux 构建动态图表应用
【10月更文挑战第3天】使用 React 和 Redux 构建动态图表应用
|
1月前
|
JavaScript 前端开发
使用 React 和 Redux 构建一个计数器应用
【10月更文挑战第3天】使用 React 和 Redux 构建一个计数器应用
|
1月前
|
存储 JavaScript 前端开发
|
1月前
|
前端开发 JavaScript 网络架构
实现动态路由与状态管理的SPA——使用React Router与Redux
【10月更文挑战第1天】实现动态路由与状态管理的SPA——使用React Router与Redux
33 1
|
3月前
|
存储 JavaScript 前端开发
React中使用redux
React中使用redux
136 56
|
1月前
|
前端开发 JavaScript
深入理解前端状态管理:React、Redux 和 MobX
【10月更文挑战第7天】深入理解前端状态管理:React、Redux 和 MobX
43 0