array(2) { ["docs"]=> array(10) { [0]=> array(10) { ["id"]=> string(3) "428" ["text"]=> string(77) "Visual Studio 2017 单独启动MSDN帮助(Microsoft Help Viewer)的方法" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(8) "DonetRen" ["tagsname"]=> string(55) "Visual Studio 2017|MSDN帮助|C#程序|.NET|Help Viewer" ["tagsid"]=> string(23) "[401,402,403,"300",404]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400964" ["_id"]=> string(3) "428" } [1]=> array(10) { ["id"]=> string(3) "427" ["text"]=> string(42) "npm -v;报错 cannot find module "wrapp"" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "zzty" ["tagsname"]=> string(50) "node.js|npm|cannot find module "wrapp“|node" ["tagsid"]=> string(19) "[398,"239",399,400]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400760" ["_id"]=> string(3) "427" } [2]=> array(10) { ["id"]=> string(3) "426" ["text"]=> string(54) "说说css中pt、px、em、rem都扮演了什么角色" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(12) "zhengqiaoyin" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511400640" ["_id"]=> string(3) "426" } [3]=> array(10) { ["id"]=> string(3) "425" ["text"]=> string(83) "深入学习JS执行--创建执行上下文(变量对象,作用域链,this)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "Ry-yuan" ["tagsname"]=> string(33) "Javascript|Javascript执行过程" ["tagsid"]=> string(13) "["169","191"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511399901" ["_id"]=> string(3) "425" } [4]=> array(10) { ["id"]=> string(3) "424" ["text"]=> string(30) "C# 排序技术研究与对比" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "vveiliang" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(8) ".Net Dev" ["catesid"]=> string(5) "[199]" ["createtime"]=> string(10) "1511399150" ["_id"]=> string(3) "424" } [5]=> array(10) { ["id"]=> string(3) "423" ["text"]=> string(72) "【算法】小白的算法笔记:快速排序算法的编码和优化" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(9) "penghuwan" ["tagsname"]=> string(6) "算法" ["tagsid"]=> string(7) "["344"]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511398109" ["_id"]=> string(3) "423" } [6]=> array(10) { ["id"]=> string(3) "422" ["text"]=> string(64) "JavaScript数据可视化编程学习(二)Flotr2,雷达图" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "chengxs" ["tagsname"]=> string(28) "数据可视化|前端学习" ["tagsid"]=> string(9) "[396,397]" ["catesname"]=> string(18) "前端基本知识" ["catesid"]=> string(5) "[198]" ["createtime"]=> string(10) "1511397800" ["_id"]=> string(3) "422" } [7]=> array(10) { ["id"]=> string(3) "421" ["text"]=> string(36) "C#表达式目录树(Expression)" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(4) "wwym" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(4) ".NET" ["catesid"]=> string(7) "["119"]" ["createtime"]=> string(10) "1511397474" ["_id"]=> string(3) "421" } [8]=> array(10) { ["id"]=> string(3) "420" ["text"]=> string(47) "数据结构 队列_队列实例:事件处理" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(7) "idreamo" ["tagsname"]=> string(40) "C语言|数据结构|队列|事件处理" ["tagsid"]=> string(23) "["246","247","248",395]" ["catesname"]=> string(12) "数据结构" ["catesid"]=> string(7) "["133"]" ["createtime"]=> string(10) "1511397279" ["_id"]=> string(3) "420" } [9]=> array(10) { ["id"]=> string(3) "419" ["text"]=> string(47) "久等了,博客园官方Android客户端发布" ["intro"]=> string(288) "目录 ECharts 异步加载 ECharts 数据可视化在过去几年中取得了巨大进展。开发人员对可视化产品的期望不再是简单的图表创建工具,而是在交互、性能、数据处理等方面有更高的要求。 chart.setOption({ color: [ " ["username"]=> string(3) "cmt" ["tagsname"]=> string(0) "" ["tagsid"]=> string(2) "[]" ["catesname"]=> string(0) "" ["catesid"]=> string(2) "[]" ["createtime"]=> string(10) "1511396549" ["_id"]=> string(3) "419" } } ["count"]=> int(200) } 222 Redux官网Counter最基本示例的思考 - 爱码网

1.不使用redux实现

如果不使用redux,仅仅依靠react去实现Counter功能是极其简单的。代码如下:
Redux官网Counter最基本示例的思考
index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Counter from './components/Counter';

const render = ()=> ReactDOM.render(
    <Counter/>,
    document.getElementById("root")
);

render();

components/Counter.js

import React, {Component} from 'react';

class Counter extends Component {
    constructor(props) {
        super(props);
        this.state = {
            counter: 0
        };
        this.add = this.add.bind(this);
        this.addOdd = this.addOdd.bind(this);
        this.addAsync = this.addAsync.bind(this);
        this.minus = this.minus.bind(this);
    }

    add() {
        this.setState({
            counter: ++this.state.counter
        })
    }

    addOdd() {
        let counter = this.state.counter;
        if (counter % 2 === 1) {
            this.setState({
                counter: ++counter
            })
        }
    }

    addAsync() {
        setTimeout(this.add, 1000);
    }

    minus() {
        this.setState({
            counter: --this.state.counter
        })
    }

    render() {
        return (
            <div>
                Clicked:{this.state.counter} times
                {' '}
                <button onClick={this.add}>+</button>
                {' '}
                <button onClick={this.minus}>-</button>
                {' '}
                <button onClick={this.addOdd}>Increment if odd</button>
                {' '}
                <button onClick={this.addAsync}>Increment async</button>
            </div>
        )
    }
}

export default Counter;

仅仅两个文件就可以实现,Counter功能。那为啥非要引入Redux呢?从自己仅有的这点react经验来看:1.本例中的状态只有counter一个,如果应用足够复杂,在组件内部自己管理状态,而这些状态又会影响其他子组件。为管理这个状态而编写的代码量将会难以阅读和维护,添加新功能也会很麻烦。2.通过用户事件去改变状态,本身没错,但是这些状态没有统一的操作去统一的state数据结构。redux就是想让状态统一管理,统一执行action操作,让 state 的变化变得可预测

2.使用redux

下面是加入redux之后的项目代码:Redux官网Counter最基本示例的思考
多了一个reducers/index.js文件。

export default (state =0,action) =>{
    switch (action.type){
        case "INCREASE":
            return state+1;
        case "DECREASE":
            return state-1;
        default:
            return state;
    }
}

src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import Counter from './components/Counter';
import {createStore} from 'redux';
import counter from './reducers';

const store = createStore(counter);

const render = ()=> ReactDOM.render(
    /*返回应用当前的 state 树,它与 store 的最后一个 reducer 返回值相同。*/
    <Counter counter={store.getState()}
        add={()=>store.dispatch({type:'INCREASE'})}
        minus={()=>store.dispatch({type:'DECREASE'})}
    />,
    document.getElementById("root")
);

render();
store.subscribe(render);

components/Counter.js

import React, {Component} from 'react';
import PropTypes from 'prop-types';


class Counter extends Component {
    constructor(props) {
        super(props);
        this.addOdd = this.addOdd.bind(this);
        this.addAsync = this.addAsync.bind(this);
    }


    addOdd() {
        let counter = this.props.counter;
        if (counter % 2 === 1) {
            this.props.add()
        }
    }

    addAsync() {
        setTimeout(this.props.add, 1000);
    }



    render() {
        const { counter, add, minus } = this.props;
        return (
            <div>
                Clicked:{counter} times
                {' '}
                <button onClick={add}>+</button>
                {' '}
                <button onClick={minus}>-</button>
                {' '}
                <button onClick={this.addOdd}>Increment if ODD</button>
                {' '}
                <button onClick={this.addAsync}>Increment async</button>
            </div>
        )
    }
}
//严格定义参数类型
Counter.propTypes = {
    counter: PropTypes.number.isRequired,
    add: PropTypes.func.isRequired,
    minus: PropTypes.func.isRequired
};

export default Counter;

在使用redux之后,Counter,中的状态管理被统一到store中,通过store.dispatch(action)的方法唯一的去修改state内容,具体的:会使用当前 getState() 的结果和传入的 action 以同步方式的调用 store 的 reduce 函数。返回值会被作为下一个 state。从现在开始,这就成为了 getState() 的返回值,同时变化监听器(change listener)会被触发。

相关文章: