美文网首页
redux基本使用

redux基本使用

作者: key君 | 来源:发表于2019-11-04 13:15 被阅读0次

redux flow:
数据放在store里面,store接收一个state和action,根据action的type值通过reducer返回一个新的state,通过action派发react components。

reducer是纯函数 接收旧的state和action 返回新的state

数组reduce

const array1 = [1,2,3,4];
const reducer = (accumulator,currentValue) => accumulator+currentValue;

console.log(array1.reduce(reducer));
console.log(array1.reduce(reducer,5));

函数聚合 按顺序执行f1 f2 f3

function f1(){
    console.log('f1');
    }
    function f2(){
    console.log('f2');
    }
    function f3(){
    console.log('f3');
    }
    function compose(...funcs){
      const len = funcs.length;
      if(len === 0){
        return arg => arg;
      }else if(len === 1){
        return funcs[0];
      }else{
        return funcs.reduce((a,b)=>(...args)=>b(a(...args)));
      }
    }
    compose(f1,f2,f3)();
Redux的使用

是js应用的状态容器。它保证程序行为一致性且易于测试。
page主要做的事情是发布消息和订阅消息
reducer做的事情是接收一个对象,里面有state和action,根本action去处理state,再返回新的state
store做的事情是使用redux的createStore函数,传入reducer,返回store实例
store.subscribe 接受订阅
store.getState 获取state的值
store.dispatch 派发事件

安装:

npm install redux --save

基本使用
src/pages/ReduxPage.js

import React, { Component } from 'react'
import store from '../store/ReduxStore';

export default class ReduxPage extends Component {
    componentDidMount(){
        //订阅
        store.subscribe(()=>{
            this.forceUpdate();
            // this.setState({});
        })
    }
    render() {
        console.log(store);
        return (
            <div>
                {/* 获取 */}
                <p>{store.getState()}</p>
                {/* 发布 */}
                <button onClick={()=>store.dispatch({type:'add'})}>add</button>
            </div>
        )
    }
}

store/counterReducer.js

export function counterReducer(state=0,action){
    switch(action.type){
        case 'add':
            return state + 1;
        case 'minus':
            return state - 1;
        default:
            return state;
    }
}
 

store/ReduxStore.js

import {createStore} from 'redux';
import {counterReducer} from './counterReducer';

const store = createStore(counterReducer);

export default store;

相关文章

  • redux 及 react-redux 的基本使用

    本文介绍 redux 的基本使用,介绍 redux 的流程。以及 react-redux 的基本使用 一、redu...

  • redux基本使用

    redux flow:数据放在store里面,store接收一个state和action,根据action的typ...

  • 玩Android(flutter + fish_redux)

    fish_redux使用 注:该项目为Flutter + fish_redux,页面基本均是fish_redux搭...

  • redux-saga框架使用详解及Demo教程

    redux-saga框架使用详解及Demo教程 前面我们讲解过redux框架和dva框架的基本使用,因为dva框架...

  • ReactNative Redux基本使用

    在RN里, 每个组件有两个属性, state 和 props, 他们都是对象, 里面可以包含多个属性 state ...

  • Redux的基本使用

    redux 是 js 应用的可预测状态的容器。 可以理解为全局数据状态管理工具,用来做组件通信。 一、安装redu...

  • redux--基本使用

    redux的工作流程 组件发出指令(actionCreator)到中间层(store),由中间层查找指令对应的...

  • 自己实现Redux

    自己实现Redux 不使用react,直接使用原生的html/js来写一个简易的的redux 基本的状态管理及数据...

  • React-Redux 使用

    目前使用版本 建议先了解 Redux 的使用!Redux使用(纯Redux使用) React-Redux 是 Re...

  • Redux原理分析以及使用详解(TS && JS)

    Redux原理分析 一、Reudx基本介绍 1.1、什么时候使用Redux? 简单说,如果你的UI层非常简单,没有...

网友评论

      本文标题:redux基本使用

      本文链接:https://www.haomeiwen.com/subject/xperbctx.html