React: 傻瓜/展示性组件的简化 (函数型组件及其传参)
来源:互联网 发布:java jdk 7 64位下载 编辑:程序博客网 时间:2024/06/06 04:54
案例代码
import React, { Component, PropTypes } from 'react';import store from '../Store.js';import * as Actions from '../Actions.js';const buttonStyle = { margin: '10px'};// 情况一: 有类// class Counter extends Component {// render() {// const {caption, onIncrement, onDecrement, value} = this.props;//// return (// <div>// <button style={buttonStyle} onClick={onIncrement}>+</button>// <button style={buttonStyle} onClick={onDecrement}>-</button>// <span>{caption} count: {value}</span>// </div>// );// }// }// 情况二: 函数式,无类;注①// function Counter(props) {// const {caption, onIncrement, onDecrement, value} = props;// return (// <div>// <button style={buttonStyle} onClick={onIncrement}>+</button>// <button style={buttonStyle} onClick={onDecrement}>-</button>// <span>{caption} count: {value}</span>// </div>// );//// }// 情况三: 直接将解构赋值放在参数部分,最简洁function Counter({caption, onIncrement, onDecrement, value}) { return ( <div> <button style={buttonStyle} onClick={onIncrement}>+</button> <button style={buttonStyle} onClick={onDecrement}>-</button> <span>{caption} count: {value}</span> </div> );}Counter.propTypes = { caption: PropTypes.string.isRequired, onIncrement: PropTypes.func.isRequired, onDecrement: PropTypes.func.isRequired, value: PropTypes.number.isRequired};class CounterContainer extends Component { constructor(props) { super(props); this.onIncrement = this.onIncrement.bind(this); this.onDecrement = this.onDecrement.bind(this); this.onChange = this.onChange.bind(this); this.getOwnState = this.getOwnState.bind(this); this.state = this.getOwnState(); } getOwnState() { return { value: store.getState()[this.props.caption] }; } onIncrement() { store.dispatch(Actions.increment(this.props.caption)); } onDecrement() { store.dispatch(Actions.decrement(this.props.caption)); } onChange() { this.setState(this.getOwnState()); } shouldComponentUpdate(nextProps, nextState) { return (nextProps.caption !== this.props.caption) || (nextState.value !== this.state.value); } componentDidMount() { store.subscribe(this.onChange); } componentWillUnmount() { store.unsubscribe(this.onChange); } render() { return <Counter caption={this.props.caption} onIncrement={this.onIncrement} onDecrement={this.onDecrement} value={this.state.value} /> }}CounterContainer.propTypes = { caption: PropTypes.string.isRequired};export default CounterContainer;
很明显,情况三最简洁。
注
- 函数式组件,不能用this.props,需要通过参数获得
- 《深入浅出React & Redux》p66
参考文章
容器组件与展示组件
阅读全文
0 0
- React: 傻瓜/展示性组件的简化 (函数型组件及其传参)
- 容器组件和展示组件react-redux
- react 函数式组件
- React 中被忽略的函数组件(Functional Components)
- React框架中组件的生命周期及其涉及到的方法
- React : 展示组件 & 容器组件 附案例与视频
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React创建组件的三种方式及其区别
- React:组件的生命周期
- React-组件的复合
- React的表单组件
- React-组件的复合
- Raspbian更新源
- jsp文件中引入js、css文件失败问题
- 本地存储的使用跟删除
- iOS基础知识5
- ucos-iii学习之前后台系统
- React: 傻瓜/展示性组件的简化 (函数型组件及其传参)
- 浏览器开发前端工程,刷新缓存问题
- 隐藏元素后占的空间没有改变
- spring+ehcache集群
- Nginx初始及优化参数及Tomcat优化
- vue2.0 组件之间的通信
- CSDN到底要多少积分才有排名(图解)
- bootstrap table 获取数据的两种方式
- Rational Rose_2007的下载、安装与破解--UML建模软件