实操《深入浅出React和Redux》第三期—Redux
来源:互联网 发布:佰笛手风琴 淘宝 编辑:程序博客网 时间:2024/05/29 09:12
尽管还没有到react-redux,但感觉已经很兴奋啦。
前端工程组件化编程思想,
确实也经历过越来越多的场景,
也越来越标准了。
这里涉及的是redux的基础应用,傻瓜和聪明组件,全局context。
就不完全收录代码了。
十点过了嘛~~
这里只记录几个重要的点吧。
Provider.js
import React from 'react';import ReactDOM from 'react-dom';import './index.css';import ControlPanel from './views/ControlPanel';import store from './Store';import Provider from './Provider';import registerServiceWorker from './registerServiceWorker';ReactDOM.render(<Provider store={store}><ControlPanel /></Provider>, document.getElementById('root'));registerServiceWorker();
Reducer.js
import * as ActionTypes from './ActionTypes';export default (state, action) => {const {counterCaption} = action;switch (action.type) {case ActionTypes.INCREMENT:return {...state, [counterCaption]: state[counterCaption] + 1};case ActionTypes.DECREMENT:return {...state, [counterCaption]: state[counterCaption] - 1};default:return state}}
Store.js
import {createStore} from 'redux';import reducer from './Reducer';const initValues = {'First': 0,'Second': 10,'Third': 20};const store = createStore(reducer, initValues);export default store;
Counter.js
import React, { Component } from 'react';import PropTypes from 'prop-types';import * as Actions from '../Actions.js';const buttonStyle = {margin: '10px'};const propTypes = {caption: PropTypes.string.isRequired};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>);}}Counter.propTypes = {caption: PropTypes.string.isRequired,onIncrement: PropTypes.func.isRequired,onDecrement: PropTypes.func.isRequired,value: PropTypes.number.isRequired};class CounterContainer extends Component {constructor(props, context) {super(props, context);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();}shouldComponentUpdate(nextProps, nextState) {return (nextProps.caption !== this.props.caption) ||(nextState.value !== this.state.value);}componentDidMount() {this.context.store.subscribe(this.onChange);}componentWillUnmout() {this.context.store.unsubscribe(this.onChange);}onChange() {this.setState(this.getOwnState());}getOwnState() {return {value: this.context.store.getState()[this.props.caption]};}onIncrement() {this.context.store.dispatch(Actions.increment(this.props.caption));}onDecrement() {this.context.store.dispatch(Actions.decrement(this.props.caption));}render() {return <Counter caption={this.props.caption}onIncrement={this.onIncrement}onDecrement={this.onDecrement}value={this.state.value} />}}CounterContainer.propTypes = {caption: PropTypes.string.isRequired};CounterContainer.contextTypes = {store: PropTypes.object}export default CounterContainer;
阅读全文
0 0
- 实操《深入浅出React和Redux》第三期—Redux
- 实操《深入浅出React和Redux》第四期--react-redux
- 实操《深入浅出React和Redux》第二期—Flux
- 实操《深入浅出React和Redux》第一期
- react知识小结——深入浅出React和Redux
- 深入浅出React之第三章:使用redux管理应用状态
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- React和Redux的连接react-redux
- react+redux+react-redux
- Redux和React
- Redux, Redux thunk 和 React Redux 源码阅读
- 深入浅出React之第六章:Redux和服务器通信
- redux react-redux
- hashMap和hashTable
- Intent意图打开电话、邮件、短信、地图、上网
- 文件字节输出流--FileOutputStream
- (0070)iOS开发之AVFoundation枚举属性注解
- SQL Server(MSSQLSERVER) 请求失败或服务未及时响应,有关详细信息,请参见事件日志或其他的适用的错误日志。
- 实操《深入浅出React和Redux》第三期—Redux
- Docker Compose进行多容器创建与日常维护
- 互联网架构,如何进行容量设计?
- C语音基础类型
- ES6学习之路5----数组方法
- 明白为什么需要什么库就添加什么库了
- XHR异步上传文件报错: “the request was rejected because no multipart boundary was found”
- win10系统modern setup host 占用磁盘使用率的解决方法
- keil中结构体用法及Error: L6200E: Symbol Track multiply defined (by arithmetic.o and main.o).解决