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

舟山公司网站建设全网营销公司排名前十

舟山公司网站建设,全网营销公司排名前十,长沙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.mmbaike.com/news/36854.html

相关文章:

  • 网站如何做微信推广小说排行榜百度
  • 国内 上市网站建设公司竞价运营是做什么的
  • 图书宣传推广方案持续优化完善防控措施
  • 海口网站建设费用枣庄网站seo
  • 淘宝现在网站建设不能发布要发布上面类目网络媒体软文案例
  • 优化官方网站设计sem竞价推广托管代运营公司
  • 明星网站设计网销平台排名
  • 捡个将军做男友啥网站能看网站推广服务报价表
  • 东西湖区网站建设公司上海网站快速排名提升
  • 先做网站后备案吗你对网络营销的理解
  • wordpress登入账户企业网站seo
  • 石家庄百度推广家庄网站建设网络舆情分析报告范文
  • 郑州做网站推seo标题优化
  • 重庆中色十二冶金建设有限公司网站百度网站排名优化软件
  • 做网站推广要会什么品牌广告投放
  • 武汉定制网页设计站内seo优化
  • 备案成功的网站清远seo
  • 已备案网站增加域名国内优秀个人网站欣赏
  • 下载类网站模板windows优化大师好不好
  • 可以注册邮箱的网站东莞网站营销
  • 做图素材网站开通会员哪个好厦门seo计费
  • wordpress 同步登陆seo教程培训班
  • 做网站哪个平台好一点无锡seo关键词排名
  • html语言做的网站和asp的区别网站建设优化推广
  • 广州 网站的设计公司佛山seo按效果付费
  • wordpress垃圾箱在哪长治网站seo
  • 计算机网站建设与维护建立网站需要什么技术
  • 专做影视评论的网站厦门seo蜘蛛屯
  • 公司网站是否必须做可信认证本周时事新闻概要10条
  • 绍兴网站建站模板如何进行新产品的推广