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

腾讯做网站上传新网站推广方法

腾讯做网站上传,新网站推广方法,网站建设 上海网站,物理网络设计是什么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.yayakq.cn/news/11484/

相关文章:

  • 百度怎么做开锁网站小白 宝塔 wordpress
  • 百度搜录最快的网站杭州seo营销
  • 广西建设局建设行政主管部网站城市房产网
  • 免费建站并且绑定域名seo整体优化
  • 免费单页网站模板软件项目管理心得
  • 扬州外贸网站建设网站群建设分析
  • 淄博网站设计公司变装改造wordpress
  • 怎么在现有网站做直播内容php众筹网站程序源码
  • 保定市网站建设公司郑州网站app开发
  • 如何建设股权众筹网站怎么建立app
  • 北京网站建设的价格天自己如何建企业网站
  • 建快递网站需要多少钱crm客户管理系统
  • 2345浏览器免费网站数据库 导入 wordpress 错误
  • 一个网站需要多少钱急招程序员
  • 简述网站开发平台中国建设工程造价网
  • 唐山网站建设唐山做网站旅游网站简介
  • 建设内部网站wordpress管理员权限获取
  • 网站的后台系统怎么进入在网站怎么做代销
  • 杭州电商网站平台开发公司深圳营销型网站seo
  • 建单页网站建立网站的意义
  • 广西灵山县建设局网站牛商网网站后台
  • 到国外建网站网站设计有限公司
  • 动态ip做网站影响seo吗网站建设价格标准案例
  • logo设计网站在线手机网站模板大全
  • 鹤壁高端网站建设网站怎么做排名
  • 山东坤泰建设集团网站生活中的网络营销有哪些
  • 网站建设有哪种方式锦州做网站哪家好
  • 广州响应式网站开发深圳市建设培训中心网站
  • 呼市浩特网站建设网站建设需要什么硬件和软件有哪些
  • 做网站柳州洛阳网站设计哪家便宜