当前位置: 首页 > news >正文

邯郸市三建建筑公司网址百度seo网站在线诊断

邯郸市三建建筑公司网址,百度seo网站在线诊断,河北中太建设集团有限公司网站,做网站有哪些公司React 基础巩固(三十二)——Redux的三大原则 一、Redux的三大原则 单一数据源 整个应用程序的state被存储在一颗object tree 中,并且这个object tree 只存储在一个store中;Redux并没有强制让我们不能创建多个Store,但是那样做不利于数据维护…

React 基础巩固(三十二)——Redux的三大原则

一、Redux的三大原则

  1. 单一数据源

    • 整个应用程序的state被存储在一颗object tree 中,并且这个object tree 只存储在一个store中;
    • Redux并没有强制让我们不能创建多个Store,但是那样做不利于数据维护;
    • 单一的数据源可以让整个应用程序的state变得方便维护、追踪、修改;
  2. State是只读的

    • 唯一修改State的方法一定是触发action,不要试图在其他地方通过任何的方式来修改State;

    • 这样就确保了View或网络请求都不能直接修改state,它们只能通过action来描述自己想要如何修改state;

    • 这样可以保证所有的修改都被集中化处理,并且按照严格的顺序来执行,所以不需要担心reace condition(竞态)的问题;

  3. 使用纯函数来执行修改

    • 通过reducer将旧state和actions联系在一起,并且返回一个新的state;
    • 随着应用程序的复杂度增加,我们可以将reducer拆分成多个小的reducers,分别操作不同state tree 的一部分;
    • 但是所有的reducer都应该是纯函数,不能产生任何的副作用;

二、Redux的使用流程

在这里插入图片描述

三、Redux的基本使用(计数器小案例)

  1. 构建react项目

    # 创建新的react项目
    create-react-app redux-learn
    # 创建成功后cd进入文件夹,随后安装redux
    npm install redux
    
  2. 删除暂时无关文件,构建store相关文件,并引用store至所需页面中

    • 目录

在这里插入图片描述

  • store/constants.js

    export const ADD_NUMBER = "add_number";
    export const SUB_NUMBER = "sub_number";
    
  • store/reducer.js

    import * as actionTypes from "./constants";const initialState = {counter: 111,
    };function reducer(state = initialState, action) {switch (action.type) {case actionTypes.ADD_NUMBER:return { ...state, counter: state.counter + action.num };case actionTypes.SUB_NUMBER:return { ...state, counter: state.counter - action.num };default:return state;}
    }export default reducer;
  • store/actionCreators.js

    import * as actionTypes from "./constants";export const addNumberAction = (num) => ({type: actionTypes.ADD_NUMBER,num,
    });export const subNumberAction = (num) => ({type: actionTypes.SUB_NUMBER,num,
    });
  • store/index.js

    import { createStore } from "redux";
    import reducer from "./reducer";const store = createStore(reducer);export default store;
  • pages/home.jsx

    import React, { PureComponent } from "react";
    import store from "../store";
    import { addNumberAction } from "../store/actionCreators";
    export class home extends PureComponent {constructor() {super();this.state = {counter: store.getState().counter,};}componentDidMount() {store.subscribe(() => {const state = store.getState();this.setState({counter: state.counter,});});}addNumber(num) {store.dispatch(addNumberAction(num));}render() {const { counter } = this.state;return (<div>home counter:{counter}<div><button onClick={(e) => this.addNumber(1)}>+1</button><button onClick={(e) => this.addNumber(5)}>+5</button><button onClick={(e) => this.addNumber(8)}>+8</button></div></div>);}
    }export default home;
  • pages/profile.jsx

    import React, { PureComponent } from "react";
    import store from "../store";
    import { subNumberAction } from "../store/actionCreators";
    export class profile extends PureComponent {constructor() {super();this.state = {counter: store.getState().counter,};}componentDidMount() {store.subscribe(() => {const state = store.getState();this.setState({counter: state.counter,});});}subNumber(num) {store.dispatch(subNumberAction(num));}render() {const { counter } = this.state;return (<div>profile counter:{counter}<div><button onClick={(e) => this.subNumber(1)}>-1</button><button onClick={(e) => this.subNumber(5)}>-5</button><button onClick={(e) => this.subNumber(8)}>-8</button></div></div>);}
    }export default profile;
  • App.jsx

    import React, { PureComponent } from "react";
    import Home from "./pages/home";
    import Profile from "./pages/profile";
    import "./style.css";
    import store from "./store";export class App extends PureComponent {constructor() {super();this.state = {counter: store.getState().counter,};}componentDidMount() {store.subscribe(() => {const state = store.getState();this.setState({counter: state.counter,});});}render() {const { counter } = this.state;return (<div><h2>App Counter: {counter}</h2><div className="pages"><Home /><Profile /></div></div>);}
    }export default App;
  1. 运行结果
    在这里插入图片描述

至此,代码仍较为复杂,代码将在React 基础巩固(三十三)中得到优化

http://www.hrbkazy.com/news/47827.html

相关文章:

  • 贵阳百度公司建网站电话重庆seo网站哪家好
  • 先学php还是网站建设推广网站最有效办法
  • 新华网两学一做专题网站奖券世界推广网站
  • 余姚网站建设公司朋友圈广告推广代理
  • 惠州网站外包品牌推广专员
  • 昆明网站制作网页推广策略怎么写
  • java配合什么做网站软文营销案例200字
  • 企业的门户网站一般用什么做设计网站
  • 怎样做网站的排名手机助手
  • 绍兴模板建站公司市场调研方案怎么写
  • 易售乐服装销售管理软件百度搜索引擎优化公司哪家强
  • 网站是什么程序做的长春网站关键词推广
  • 秦时明月的个人网站怎么做seo有哪些优化工具
  • 普陀区网站建设前端seo博客教程
  • 网站优化培训学校靠谱的seo收费
  • 怎样加入好大夫网站做医生品牌运营包括哪些内容
  • 建设工程挂网是在那个网站微信公众号运营推广方案
  • 秦皇岛手机网站制作深圳网站优化软件
  • 企业h5网站建设买域名
  • 如何查看网站建设的时间首码项目推广平台
  • 电子商务网站建设与管理理解5g站长工具seo综合查询
  • 做网站需要哪些关键字排名查询工具
  • 做360手机网站百度网盘电脑版登录入口
  • 公司网站工程案例怎么做深圳seo公司排名
  • 专业app网站建设社交网络的推广方法有哪些
  • 筑巢网络官方网站广州网站排名优化公司
  • wordpress原创meta新网站百度seo如何做
  • php动态网站开发唐四薪课后答案百度指数查询移民
  • 使用wordpress做网站windows优化大师是什么
  • 动态网页设计技术有哪些东莞seo优化seo关键词