实操《深入浅出React和Redux》第一期

上次的书看得差不多了,但实践越来越麻烦。

于是重新开一本书,这次,人家用了create-react-app。

实践方面就容易开展啦。:)

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

主要文件内容也少多啦。

index.js

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

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

ReactDOM.render(<ControlPanel />,document.getElementById('root'));
registerServiceWorker();
ControlPanel.js
import React,{ Component } from 'react';
import Counter from './Counter';

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

class ControlPanel extends Component {
	constructor(props){
		super(props);
		this.onCounterUpdate = this.onCounterUpdate.bind(this);
		this.initValue = [0,10,20];
		const initSum = this.initValue.reduce((a,b) => a+b,0);
		this.state = {
			sum: initSum
		};
	}
	
	onCounterUpdate(newValue,prevIoUsValue) {
		const valueChange = newValue - prevIoUsValue;
		this.setState({sum: this.state.sum + valueChange});
	}
	render() {
		console.log("enter ControlPanel render");
		return (
			<div style={style}>
				<Counter onUpdate={this.onCounterUpdate} caption="First" />
				<Counter onUpdate={this.onCounterUpdate} caption="Second" initValue = {10} />
				<Counter onUpdate={this.onCounterUpdate} caption="Third" initValue = {20} />
				<button onClick={ ()=> this.forceUpdate() }>
					Click me to re-render!
				</button>
				<hr />
				<div> Total Count: {this.state.sum}</div>
			</div>
		);
	}
}

export default ControlPanel;
Counter.js Counter.js
import React,{ Component } from 'react'; import PropTypes from 'prop-types'; const buttonStyle = { margin: '10px' }; const propTypes = { caption: PropTypes.string.isrequired,initValue: PropTypes.number,onUpdate: PropTypes.func }; class Counter extends Component { constructor(props) { super(props); console.log("enter constructor: " + props.caption); this.onClickIncrementButton = this.onClickIncrementButton.bind(this); this.onClickDecrementButton = this.onClickDecrementButton.bind(this); this.state = { count: props.initValue } } onClickIncrementButton() { this.updateCount(true); } onClickDecrementButton() { this.updateCount(false); } updateCount(isIncrement) { const prevIoUsValue = this.state.count; const newValue = isIncrement?prevIoUsValue + 1:prevIoUsValue -1; this.setState({count: newValue}); this.props.onUpdate(newValue,prevIoUsValue); } render() { console.log("enter render " + this.props.caption); 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;import React,onUpdate: f => f }; Counter.propTypes = propTypes export default Counter;import React,onUpdate: f => f }; Counter.propTypes = propTypes export default Counter;

阅读原文

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

相关文章

react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...
react 本身提供了克隆组件的方法,但是平时开发中可能很少使...
mobx 是一个简单可扩展的状态管理库,中文官网链接。小编在接...
我们在平常的开发中不可避免的会有很多列表渲染逻辑,在 pc ...