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

前端之家收集整理的这篇文章主要介绍了实操《深入浅出React和Redux》第一期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

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

主要文件内容也少多啦。

index.js

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4.  
  5. import ControlPanel from './ControlPanel';
  6. import registerServiceWorker from './registerServiceWorker';
  7.  
  8. ReactDOM.render(<ControlPanel />,document.getElementById('root'));
  9. registerServiceWorker();
  1. ControlPanel.js
  1. import React,{ Component } from 'react';
  2. import Counter from './Counter';
  3.  
  4. const style = {
  5. margin: '20px'
  6. };
  7.  
  8. class ControlPanel extends Component {
  9. constructor(props){
  10. super(props);
  11. this.onCounterUpdate = this.onCounterUpdate.bind(this);
  12. this.initValue = [0,10,20];
  13. const initSum = this.initValue.reduce((a,b) => a+b,0);
  14. this.state = {
  15. sum: initSum
  16. };
  17. }
  18. onCounterUpdate(newValue,prevIoUsValue) {
  19. const valueChange = newValue - prevIoUsValue;
  20. this.setState({sum: this.state.sum + valueChange});
  21. }
  22. render() {
  23. console.log("enter ControlPanel render");
  24. return (
  25. <div style={style}>
  26. <Counter onUpdate={this.onCounterUpdate} caption="First" />
  27. <Counter onUpdate={this.onCounterUpdate} caption="Second" initValue = {10} />
  28. <Counter onUpdate={this.onCounterUpdate} caption="Third" initValue = {20} />
  29. <button onClick={ ()=> this.forceUpdate() }>
  30. Click me to re-render!
  31. </button>
  32. <hr />
  33. <div> Total Count: {this.state.sum}</div>
  34. </div>
  35. );
  36. }
  37. }
  38.  
  39. export default ControlPanel;
  1. Counter.js Counter.js
  1. 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相关文章