zoukankan      html  css  js  c++  java
  • redux工程化结构

    一、简述

    redux的工程化管理

    • 1.reducer的模块化划分:每一个板块有一个自己对应的reducer,最后基于一些方法把所以的reducer合并即可;
    • 2.基于actionCreator统一管理每次派发需要的行为对象,而且和reducer一样,也是分板块管理的;
    • 3.把dispatch和reducer校验时候需要的行为标识(type)进行统一管理

    目录建设

    store store中存放的是redux中使用的东西

    • action: action文件夹存放的是actionCreator内容
    • reducer reducer文件夹存放的是每个板块自己对应的reducer
    • actionTypes.js 存储项目中需要的所以行为标识
    • index.js 创建store容器的

    image

    二、实战代码

    直接看代码

    App.js //(只用引入一行创建容器store的js就可以了)
    import './store/index.js'
    
    store/index.js  //(创建容器,传入合并好的reducer)
    import {createStore} from 'redux'
    import reducer from './reducer/index.js'
    let store = createStore(reducer);
    window.store = store;
    
    store/actionTypes.js //(定义所有的类型变量名)
    const VOTE_SUPPORT  = 'VOTE_SUPPORT';
    const VOTE_AGAINST  = 'VOTE_AGAINST';
    export {
        VOTE_SUPPORT,
        VOTE_AGAINST
    }
    
    store/action/vote.js //(按不同模块定义的不同的行为对象,返回相应的标识type类型)
    import * as TYPES from '../actionTypes.js'
    let vote = {
        support(){
            return {
                type: TYPES.VOTE_SUPPORT
            }
        },
        against(){
            return {
                type: TYPES.VOTE_AGAINST
            }
        }
    };
    export default vote;
    
    store/action/index.js //(导出不同模块的action对象)
    import vote from './vote'
    import person from './person'
    export default {    
        vote,
        person
    }
    
    store/reducer/vote.js //(定义不同模块的自己的reducer)
    import * as TYPES from '../actionTypes.js'
    export default function vote(state = {
        n: 0,
        m: 0
    }, action) {
        switch (action.type) {
            case TYPES.VOTE_SUPPORT: state.n = state.n + 1; break;
            case TYPES.VOTE_AGAINST: state.m = state.m + 1; break;
        }
        return state;
    }
    
    store/reducer/index.js //(利用combineReducers函数合并不同的reducer)
    import vote from './vote'
    import person from './person'
    import {combineReducers} from 'redux'
    let reducer = combineReducers({
        vote,
        person
    });
    export default reducer;
    
    <Vote title={'标题一'}></Vote>  //调用
    
    vote.js
    import React from 'react';
    import VoteBody from './voteBody.js'
    import VoteFooter from './voteFooter.js'
    class Vote extends React.Component{
        constructor(){
            super();
        }
        render() {
            return (
                <div>
                    <VoteBody></VoteBody>
                    <VoteFooter></VoteFooter>
                </div>
            )
        }
    }
    export default Vote;
    
    voteFooter.js
    import React from 'react';
    import action from '../store/action/index.js'
    class VoteFooter extends React.Component{
        constructor(){
            super()
        }
        render() {
            //获取不同模块自己的行为函数,执行后获取对应的标识
            let {support, against} = action.vote;   
            return (
                <div>
                    <button onClick={e => window.store.dispatch(support())}>赞成</button>
                    <button onClick={e => window.store.dispatch(against())}>反对</button>
                </div>
            )
        }
    }
    export default VoteFooter;
    
    VoteBody.js
    import React from 'react';
    class VoteBody extends React.Component{
        constructor(){
            super()
        }
        componentDidMount() {
            window.store.subscribe(()=>{
                this.forceUpdate();
            })
        }
        render() {
            //获取不同模块自己的state
            let {n, m} = window.store.getState().vote; 
            return (
                <div>
                    <div>赞成{n}票</div>
                    <div>反对{m}票</div>
                </div>
            )
        }
    }
    export default VoteBody;
  • 相关阅读:
    DDOS攻击与防御
    .NET Core 二维码生成Demo
    腾讯云服务器远程登录卡顿严重导致无法登录解决办法
    signalR【一、官方案例】
    Python题目:生成100个随机不重复的字符串【杭州多测师】【杭州多测师_王sir】
    把字符串当中重复的字符打印出来【杭州多测师】【杭州多测师_王sir】
    PC#1 ping PC#2,请描述PC1和PC2之间的通信过程【杭州多测师】【杭州多测师_王sir】
    逻辑题【杭州多测师】【杭州多测师_王sir】
    编程题:给一个网址获取分配网址的协议,域名,路径,端口,参数1的值,参数2的值,并以字典的形式打印出结果【杭州多测师】【杭州多测师_王sir】
    经典的Python题目之找到a开头的元素、组成新的列表【多测师王sir】【杭州多测师_王sir】
  • 原文地址:https://www.cnblogs.com/chaixiaozhi/p/12215116.html
Copyright © 2011-2022 走看看