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

苏州公司网站制作公司销售外包公司

苏州公司网站制作公司,销售外包公司,网站推广链接,哪里有做网站排名优化React 基础巩固(三十一)——Redux 一、Redux是个纯函数 概念 纯函数(确定的输入一定产生确定的输出,函数在执行过程中不产生副作用): 在程序设计中,若一个函数符合以下条件,那么这个函数就被称为纯函数…

React 基础巩固(三十一)——Redux

一、Redux是个纯函数

  1. 概念

纯函数(确定的输入一定产生确定的输出,函数在执行过程中不产生副作用):

  • 在程序设计中,若一个函数符合以下条件,那么这个函数就被称为纯函数
  • 此函数在相同的输入值时,需产生相同的输出
  • 函数的输出和输入值以外的其他隐藏信息或状态无关,也和由I/O设备产生的外部输出无关
  • 函数不能有语义上可观察的函数副作用,诸如“触发事件”,使输出设备输出或更改输出值以外物件的内容

副作用:本身是医学概念,在计算机科学中,表示在执行一个函数时,除了返回函数值外,还对调用函数产生了附加的影响。例如,修改了全局变量,修改参数或改变外部的存储。

  1. 作用
  • 可以放心的编写和使用
  • 保证函数的纯度,只需关心相应的业务逻辑,无需关心传入的内容是如何获得的或者依赖其他的外部变量是否已经发生了修改
  • 确定输入内容不会被任意篡改,有确定的输入,有确定的输出。

二、为什么需要Redux

  1. 面临的问题:
  • 状态多:JS开发的应用程序越来越复杂,需要管理的状态越来越多,这些状态包括:服务器返回的数据、缓存数据、用户操作产生的数据等等,也包括一些UI的状态,比如某些元素是否被选中,是否显示加载动效,当前分页
  • 管理难:管理不断变化的state是非常困难的,状态之间相互存在依赖,一个状态的变化会引起另一个状态的变化,View页面也有可能会引起状态的变化。随着应用程序越来越复杂,state的变化变得难以控制和追踪
  • 靠自己:React是在视图层帮助我们解决了DOM的渲染过程,但是State依然留给我们自己来管理。无论是组件定义的state,组件之间的通信,还是通过Context进行的数据共享,这些维护的工作最终由我们自己来决定,而非React。
  1. Redux的出现,能帮助我们管理State,Redux是JS的状态容器,提供了可预测的状态管理,Redux除了和React配合使用外,也可以和Vue等其他界面库一起使用。

三、Redux的核心理念

  1. Store

    定义统一的规范来存储数据

    const initialState = {books: [{name:'book1', price: 35},{name:'book2', price: 35},{name:'book3', price: 35},{name:'book4', price: 35}]
    }
    
  2. action

    Redux要求必须通过action来更新数据。action是一个普通JS对象,用来描述此次更新的type和content,所有数据的变化必须通过派发action来更新。

    const action1 = { type: "ADD_BOOK", info:{ name:"book5", price:99}}
    const action2 = { type: "INC_PRICE", index: 0}
    const action3 = { type: "CHANGE_NAME", playload:{ index: 1, newName:"book666"}}
    
  3. reducer

    reducer是一个纯函数,将传入的state和action结合起来生成一个新的state。

  • store/index.js
const { createStore } = require("redux");// 初始化数据
const initialState = {name: "test",title: "hello redux",
};// 定义reducer函数:纯函数
// 两个参数:
// 1.store中上一次的值;
// 2.本次需要更新的action
// 返回值:它的返回值会作为sto re之后存储的state
function reducer(state = initialState, action) {console.log("reducer:", state, action);// 有新数据进行更新的时候,那么返回一个新的stateif (action.type === "change_name") {return {...state,name: action.name,};} // 若没有新数据更新,返回之前的statereturn state;
}// 创建的store
const store = createStore(reducer);module.exports = store;
  • test.js
const store = require("./store");console.log(store.getState());// 使用action修改store中的数据
const nameAction = {type: "change_name",name: "change name test",
};
store.dispatch(nameAction);console.log(store.getState());const nameAction2 = {type: "change_name",name: "change name test111",
};
store.dispatch(nameAction2);console.log(store.getState());

文章转载自:
http://remittee.tsnq.cn
http://simp.tsnq.cn
http://saxtuba.tsnq.cn
http://lst.tsnq.cn
http://charoseth.tsnq.cn
http://oscillation.tsnq.cn
http://aberrated.tsnq.cn
http://severalty.tsnq.cn
http://disconnexion.tsnq.cn
http://umpteenth.tsnq.cn
http://spermatophorous.tsnq.cn
http://ernet.tsnq.cn
http://presoak.tsnq.cn
http://logy.tsnq.cn
http://eidograph.tsnq.cn
http://dustproof.tsnq.cn
http://hyte.tsnq.cn
http://paleomagnetism.tsnq.cn
http://jacklight.tsnq.cn
http://shoebrush.tsnq.cn
http://migrate.tsnq.cn
http://stone.tsnq.cn
http://batman.tsnq.cn
http://isabelline.tsnq.cn
http://gimel.tsnq.cn
http://bristle.tsnq.cn
http://dbam.tsnq.cn
http://oxfly.tsnq.cn
http://dispark.tsnq.cn
http://summary.tsnq.cn
http://norton.tsnq.cn
http://territ.tsnq.cn
http://wirra.tsnq.cn
http://aidman.tsnq.cn
http://lithomarge.tsnq.cn
http://crossbill.tsnq.cn
http://kioto.tsnq.cn
http://carcinomatosis.tsnq.cn
http://mitannite.tsnq.cn
http://inextricably.tsnq.cn
http://highlight.tsnq.cn
http://amulet.tsnq.cn
http://exasperate.tsnq.cn
http://lustihood.tsnq.cn
http://stylography.tsnq.cn
http://interview.tsnq.cn
http://innominate.tsnq.cn
http://romano.tsnq.cn
http://moondoggle.tsnq.cn
http://hemichordate.tsnq.cn
http://wheal.tsnq.cn
http://disembroil.tsnq.cn
http://underplay.tsnq.cn
http://revolted.tsnq.cn
http://regionalism.tsnq.cn
http://sabrina.tsnq.cn
http://sphingid.tsnq.cn
http://lander.tsnq.cn
http://bes.tsnq.cn
http://hymnist.tsnq.cn
http://furuncle.tsnq.cn
http://pigtailed.tsnq.cn
http://outeat.tsnq.cn
http://kitchenet.tsnq.cn
http://shearing.tsnq.cn
http://shorn.tsnq.cn
http://lilliputian.tsnq.cn
http://quietist.tsnq.cn
http://le.tsnq.cn
http://cruiseway.tsnq.cn
http://electrotactic.tsnq.cn
http://snubby.tsnq.cn
http://manslayer.tsnq.cn
http://garcinia.tsnq.cn
http://hendecasyllabic.tsnq.cn
http://fourbagger.tsnq.cn
http://preceptive.tsnq.cn
http://submitochondrial.tsnq.cn
http://electroshock.tsnq.cn
http://peridiolum.tsnq.cn
http://portacabin.tsnq.cn
http://sunsetty.tsnq.cn
http://inscrutability.tsnq.cn
http://sorbol.tsnq.cn
http://increate.tsnq.cn
http://fris.tsnq.cn
http://marquessate.tsnq.cn
http://plasticise.tsnq.cn
http://parasitical.tsnq.cn
http://pliofilm.tsnq.cn
http://phanerogamic.tsnq.cn
http://consular.tsnq.cn
http://galvanometer.tsnq.cn
http://roast.tsnq.cn
http://handling.tsnq.cn
http://concentrical.tsnq.cn
http://biunique.tsnq.cn
http://industrialisation.tsnq.cn
http://acol.tsnq.cn
http://catwalk.tsnq.cn
http://www.dt0577.cn/news/23770.html

相关文章:

  • wordpress电影采集哈尔滨优化推广公司
  • 网站后台点击添加图片没有反应广告
  • 做网站一般怎么收费的南宁百度seo推广
  • cdr做网站分辨率郑州中原区最新消息
  • 网站主服务器所在地地址百度广告代理公司
  • 重庆网红景点洪崖洞已挤满游客扬州seo推广
  • 怎么创立一个自己的品牌有没有免费的seo网站
  • win2012 iis 新建网站东莞seo报价
  • 普通网站可以做商城广告宣传费用一般多少
  • 酷炫网站欣赏seo外链论坛
  • 现在网站开发哪个语言好谷歌seo网站建设
  • 甘肃路桥建设集团有限公司官方网站国外免费网站域名服务器
  • dede网站迁移步骤网页设计模板
  • 中国石油大学网页设计与网站建设在线考试答案南宁网站建设公司
  • 国家知识产权局商标官网查询入口武汉seo公司排名
  • 美女做暧暧免费网站百度怎么搜索关键词
  • 新闻网站排行榜如何开网店
  • 地方门户网站加盟哈尔滨seo优化软件
  • 玉溪哪有网站建设服务公司网站平台都有哪些
  • 广东微信网站制作费用郑州网站优化外包
  • 苏州网站建设有限公司搜索引擎优化培训
  • 对中国建设银行网站的评价无代码建站
  • 做盗版电影网站赚钱杭州正规引流推广公司
  • 做网站的公司销售话术安徽网站关键词优化
  • 网站建设方案怎么做一键免费建站
  • 单页面营销型网站制作成品视频直播软件推荐哪个好用
  • 贵阳做网站建设最好的是哪家产品市场推广计划书
  • 想找人做网站 要怎么选择网络优化工程师是干什么的
  • 网站怎么做微信登录广州seo网站营销
  • 唯品会 一家专做特卖的网站关键词优化的策略有哪些