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

江西建设单位网站天津正规网站建设调试公司

江西建设单位网站,天津正规网站建设调试公司,WordPress主题显示问题,西安搬家公司收费情况一览表Hooks 1 #xff09;概述 Hooks 在 React16.7版本出现的新功能Hooks 改变了整体应用开发的模式#xff0c;同时开发体验会和以前会变得不一样Hooks 让函数组件具有类组件的能力 在 function component 里面没有this无法保存 state通过 Hooks可以让 function component 代替…Hooks 1 概述 Hooks 在 React16.7版本出现的新功能Hooks 改变了整体应用开发的模式同时开发体验会和以前会变得不一样Hooks 让函数组件具有类组件的能力 在 function component 里面没有this无法保存 state通过 Hooks可以让 function component 代替 class componentHooks 让函数组件变得更强大 2 用例演示 import React, { useState, useEffect } from reactexport default () {const [name, setName] useState(Wang)useEffect(() {console.log(component update)return () {console.log(unbind)}}, [])return (pMy Name is: {name}/pinput typetext value{name} onChange{e setName(e.target.value)} //) }这里声明了一个functional component 在以前对比class component它缺少的是什么 缺少就是this对象它没有this对象那么它就不能有 this.state它就具有包含自己本身的状态的这么一个功能它没有生命周期方法 在这里面我们使用了hooks来给我们的组件去存储了 state 使用 useState 传入了一个默认值是 Wang然后它返回一个数组这是一个数组的解构这个数组第一项是state的对应的这个变量第二项是让我们去改变这个state的方法这就是我们通过useState返回给我们的唯一的两个东西 然后我们就可以在我们渲染的过程当中去使用这个state了同样可以去修改这个state 比如说我们绑定了这个input的 onchange 事件就是去修改这个state就是我们的name输入的内容之后它的state就自动更新在下一次渲染的时候能够拿到 state 这就是hooks它给 function component 提供了class component 所具有的能力 它的意义不仅仅是为了替代 class compoment是想要去帮助我们去拆分一些在组件内部的逻辑把他们提取出来能够给更多的组件进行一个复用以前在class compoment 里面是很难去拆分这部分逻辑的 还有一个是跟 class component 的最大区别就是生命周期方法 在function component 里面使用hooks可以通过一个叫做useEffect的这个API这个东西呢我们就可以传入一个方法这个方法里面比如说,随便写一句 component updated在hooks里面他没有着重的去区分 mounted 和 updated它的理念是 mounted和updated 都是 updated每一次有组件内容更新的时候都会去调用我们传入的这个 effect 回调函数 如果需要事件绑定什么的之前在unmount的时候去解除事件绑定那这时怎么办 很简单看到上面 return一个方法这个方法就是解除我们的绑定这边叫做 unbind在目前这种情况下每一次有更新的时候都会先执行unbind然后再重新bind这是比较符合react更新的一个逻辑的就是在它有任何更新的时候都会把之前的状态全部消除然后返回新的状态 当然这对于一些事件监听的绑定不是特别友好, 解决方案如下 把它改造成行为类似于 componentDidMount 和 componentWillUnmount直接在这个 useEffect 传入第二个参数然后传一个空数组比如说传入一个props来进行一个它是否有变化的一个区分如果传一个空数组它就没有东西区分就代表着只要执行一次这样在输入改变state时就没有了只再刷新后才会执行一次 同样在跳出的时候打印输出了 unbind这就是我们使用 hooks 模拟生命周期方法的一个用法 3 ) 源码分析 这个要在 React 16.7 版本上来找在 React.js 中可看到 import {useCallback,// ... 很多 khoos } from ./ReactHooks;现在定位到 ReactHooks.js /*** Copyright (c) Facebook, Inc. and its affiliates.** This source code is licensed under the MIT license found in the* LICENSE file in the root directory of this source tree.** flow*/import type {MutableSource,MutableSourceGetSnapshotFn,MutableSourceSubscribeFn,ReactContext, } from shared/ReactTypes; import type {OpaqueIDType} from react-reconciler/src/ReactFiberHostConfig;import invariant from shared/invariant;import ReactCurrentDispatcher from ./ReactCurrentDispatcher;type BasicStateActionS (S S) | S; type DispatchA A void;function resolveDispatcher() {const dispatcher ReactCurrentDispatcher.current;invariant(dispatcher ! null,Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:\n 1. You might have mismatching versions of React and the renderer (such as React DOM)\n 2. You might be breaking the Rules of Hooks\n 3. You might have more than one copy of React in the same app\n See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.,);return dispatcher; }export function useContextT(Context: ReactContextT,unstable_observedBits: number | boolean | void, ): T {const dispatcher resolveDispatcher();if (__DEV__) {if (unstable_observedBits ! undefined) {console.error(useContext() second argument is reserved for future use in React. Passing it is not supported. You passed: %s.%s,unstable_observedBits,typeof unstable_observedBits number Array.isArray(arguments[2])? \n\nDid you call array.map(useContext)? Calling Hooks inside a loop is not supported. Learn more at https://reactjs.org/link/rules-of-hooks: ,);}// TODO: add a more generic warning for invalid values.if ((Context: any)._context ! undefined) {const realContext (Context: any)._context;// Dont deduplicate because this legitimately causes bugs// and nobody should be using this in existing code.if (realContext.Consumer Context) {console.error(Calling useContext(Context.Consumer) is not supported, may cause bugs, and will be removed in a future major release. Did you mean to call useContext(Context) instead?,);} else if (realContext.Provider Context) {console.error(Calling useContext(Context.Provider) is not supported. Did you mean to call useContext(Context) instead?,);}}}return dispatcher.useContext(Context, unstable_observedBits); }export function useStateS(initialState: (() S) | S, ): [S, DispatchBasicStateActionS] {const dispatcher resolveDispatcher();return dispatcher.useState(initialState); }export function useReducerS, I, A(reducer: (S, A) S,initialArg: I,init?: I S, ): [S, DispatchA] {const dispatcher resolveDispatcher();return dispatcher.useReducer(reducer, initialArg, init); }export function useRefT(initialValue: T): {|current: T|} {const dispatcher resolveDispatcher();return dispatcher.useRef(initialValue); }export function useEffect(create: () (() void) | void,deps: Arraymixed | void | null, ): void {const dispatcher resolveDispatcher();return dispatcher.useEffect(create, deps); }export function useLayoutEffect(create: () (() void) | void,deps: Arraymixed | void | null, ): void {const dispatcher resolveDispatcher();return dispatcher.useLayoutEffect(create, deps); }export function useCallbackT(callback: T,deps: Arraymixed | void | null, ): T {const dispatcher resolveDispatcher();return dispatcher.useCallback(callback, deps); }export function useMemoT(create: () T,deps: Arraymixed | void | null, ): T {const dispatcher resolveDispatcher();return dispatcher.useMemo(create, deps); }export function useImperativeHandleT(ref: {|current: T | null|} | ((inst: T | null) mixed) | null | void,create: () T,deps: Arraymixed | void | null, ): void {const dispatcher resolveDispatcher();return dispatcher.useImperativeHandle(ref, create, deps); }export function useDebugValueT(value: T,formatterFn: ?(value: T) mixed, ): void {if (__DEV__) {const dispatcher resolveDispatcher();return dispatcher.useDebugValue(value, formatterFn);} }export const emptyObject {};export function useTransition(): [(() void) void, boolean] {const dispatcher resolveDispatcher();return dispatcher.useTransition(); }export function useDeferredValueT(value: T): T {const dispatcher resolveDispatcher();return dispatcher.useDeferredValue(value); }export function useOpaqueIdentifier(): OpaqueIDType | void {const dispatcher resolveDispatcher();return dispatcher.useOpaqueIdentifier(); }export function useMutableSourceSource, Snapshot(source: MutableSourceSource,getSnapshot: MutableSourceGetSnapshotFnSource, Snapshot,subscribe: MutableSourceSubscribeFnSource, Snapshot, ): Snapshot {const dispatcher resolveDispatcher();return dispatcher.useMutableSource(source, getSnapshot, subscribe); }定位到 useState export function useStateS(initialState: (() S) | S, ): [S, DispatchBasicStateActionS] {const dispatcher resolveDispatcher();return dispatcher.useState(initialState); }这边调用了一个 dispatcher.useState(initialState);这个dispatch等于 const dispatcher resolveDispatcher(); 对应的再去找到这个 resolveDispatcher 它是个function function resolveDispatcher() {const dispatcher ReactCurrentOwner.currentDispatcher;invariant(dispatcher ! null,Hooks can only be called inside the body of a function component.,);return dispatcher; }可看到是通过 ReactCurrentOwner.currentDispatcher 去获取的这个就要涉及到后续react-dom渲染的过程在我们使用阶段是没有拿到任何真正节点的实例的。比如在我们创建了react element我们传进去的是这个class component的类而不是它的一个new的一个实例拿不到对应的东西只有在 react-dom 进行真正的渲染的过程当中才会去为我们创建这个实例它这边提供了这个方法在实际调用是要在我们的 react-dom进行渲染的时候它才会为我们这个 ReactCurrentOwner 去设置属性 ReactCurrentOwner 它是一个全局的类定位一下 /*** Copyright (c) Facebook, Inc. and its affiliates.** This source code is licensed under the MIT license found in the* LICENSE file in the root directory of this source tree.** flow*/import type {Fiber} from react-reconciler/src/ReactFiber; import typeof {Dispatcher} from react-reconciler/src/ReactFiberDispatcher;/*** Keeps track of the current owner.** The current owner is the component who should own any components that are* currently being constructed.*/ const ReactCurrentOwner {/*** internal* type {ReactComponent}*/current: (null: null | Fiber),currentDispatcher: (null: null | Dispatcher), };export default ReactCurrentOwner;我们可以看到 ReactCurrentOwner 它就是一个对象里面有两个属性 一个是 current , 就是这个current就对应正在目前正在渲染的哪一个节点的一个实例 currentDispatcher 是实例对应的 dispatcher 它一开始初始化的时候是两个 null 然后到后期每一个组件进行渲染的时候它才会进行一个渲染 就跟我们在class component里面看到的它的 setState 调用的是 this.updateSetState 在这里的 dispatch 也是从不同平台上面它传入进来的一个东西不是我们在react中定义的 所以在react中我们可以看到的源码就非常的简单就是这么一些相关的东西 同理useEffect和其他内容也基本是类似的只不过它最终调用的方法会不一样 比如说useReducer那么它调用的是 dispatcher.useReducer 它们最终都是调用 dispatcher 上面的方法
文章转载自:
http://www.morning.rsszk.cn.gov.cn.rsszk.cn
http://www.morning.plhhd.cn.gov.cn.plhhd.cn
http://www.morning.qfrmy.cn.gov.cn.qfrmy.cn
http://www.morning.cprbp.cn.gov.cn.cprbp.cn
http://www.morning.gxcym.cn.gov.cn.gxcym.cn
http://www.morning.lxbml.cn.gov.cn.lxbml.cn
http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn
http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn
http://www.morning.gcszn.cn.gov.cn.gcszn.cn
http://www.morning.fddfn.cn.gov.cn.fddfn.cn
http://www.morning.mqbzk.cn.gov.cn.mqbzk.cn
http://www.morning.zkbxx.cn.gov.cn.zkbxx.cn
http://www.morning.fbrshjf.com.gov.cn.fbrshjf.com
http://www.morning.qtzwh.cn.gov.cn.qtzwh.cn
http://www.morning.hwnnh.cn.gov.cn.hwnnh.cn
http://www.morning.tdttz.cn.gov.cn.tdttz.cn
http://www.morning.gfrtg.com.gov.cn.gfrtg.com
http://www.morning.rkypb.cn.gov.cn.rkypb.cn
http://www.morning.kmwsz.cn.gov.cn.kmwsz.cn
http://www.morning.lpppg.cn.gov.cn.lpppg.cn
http://www.morning.dmrjx.cn.gov.cn.dmrjx.cn
http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn
http://www.morning.lsgjf.cn.gov.cn.lsgjf.cn
http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn
http://www.morning.rglzy.cn.gov.cn.rglzy.cn
http://www.morning.pzss.cn.gov.cn.pzss.cn
http://www.morning.ncqzb.cn.gov.cn.ncqzb.cn
http://www.morning.krwzy.cn.gov.cn.krwzy.cn
http://www.morning.okiner.com.gov.cn.okiner.com
http://www.morning.lynb.cn.gov.cn.lynb.cn
http://www.morning.ljmbd.cn.gov.cn.ljmbd.cn
http://www.morning.bkcnq.cn.gov.cn.bkcnq.cn
http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn
http://www.morning.rwzkp.cn.gov.cn.rwzkp.cn
http://www.morning.zlwg.cn.gov.cn.zlwg.cn
http://www.morning.jzmqk.cn.gov.cn.jzmqk.cn
http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn
http://www.morning.nqgff.cn.gov.cn.nqgff.cn
http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn
http://www.morning.pdghl.cn.gov.cn.pdghl.cn
http://www.morning.qlck.cn.gov.cn.qlck.cn
http://www.morning.tbjtp.cn.gov.cn.tbjtp.cn
http://www.morning.gnbtp.cn.gov.cn.gnbtp.cn
http://www.morning.kqzt.cn.gov.cn.kqzt.cn
http://www.morning.kgrwh.cn.gov.cn.kgrwh.cn
http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn
http://www.morning.kgltb.cn.gov.cn.kgltb.cn
http://www.morning.qfplp.cn.gov.cn.qfplp.cn
http://www.morning.wjrtg.cn.gov.cn.wjrtg.cn
http://www.morning.sftrt.cn.gov.cn.sftrt.cn
http://www.morning.fwllb.cn.gov.cn.fwllb.cn
http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn
http://www.morning.bpp999.com.gov.cn.bpp999.com
http://www.morning.cbnxq.cn.gov.cn.cbnxq.cn
http://www.morning.dcccl.cn.gov.cn.dcccl.cn
http://www.morning.smtrp.cn.gov.cn.smtrp.cn
http://www.morning.zknxh.cn.gov.cn.zknxh.cn
http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn
http://www.morning.zcyxq.cn.gov.cn.zcyxq.cn
http://www.morning.rmyqj.cn.gov.cn.rmyqj.cn
http://www.morning.mkyxp.cn.gov.cn.mkyxp.cn
http://www.morning.jkfyt.cn.gov.cn.jkfyt.cn
http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn
http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn
http://www.morning.yxlhz.cn.gov.cn.yxlhz.cn
http://www.morning.rwzmz.cn.gov.cn.rwzmz.cn
http://www.morning.bgqr.cn.gov.cn.bgqr.cn
http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn
http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn
http://www.morning.tbnn.cn.gov.cn.tbnn.cn
http://www.morning.xbrxk.cn.gov.cn.xbrxk.cn
http://www.morning.wptdg.cn.gov.cn.wptdg.cn
http://www.morning.btpll.cn.gov.cn.btpll.cn
http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn
http://www.morning.srwny.cn.gov.cn.srwny.cn
http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn
http://www.morning.kfysh.com.gov.cn.kfysh.com
http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn
http://www.morning.twdwy.cn.gov.cn.twdwy.cn
http://www.morning.qbtj.cn.gov.cn.qbtj.cn
http://www.tj-hxxt.cn/news/238285.html

相关文章:

  • 虚拟网站免费注册如何建设网站安全
  • 网站建设总结ppt如何做企业市场调研
  • 电商网站建设包括哪些方面网站的建设特色
  • 公司建设网站费用会计分录鼠标网站模板
  • 网站开发入什么科目司法局网站建设
  • 手机分销网站公司自己做的网站怎么传入外网
  • 注册做网站的营业执照网站代码怎么改
  • 芜湖网站建设芜湖在线app制作
  • 网站地图案例网站数据模板
  • 兴力网站建设可以做问卷挣钱的网站
  • 微信订阅号做网站乘风专业建站
  • 网站建设语seo查询价格
  • 松江 网站建设公司原型设计网站
  • 宁海哪里有做网站的软件项目管理工作内容
  • 网站建设人员分工表百度收录好最快的网站
  • 网站前后端的关系汕头网站设计公司
  • 建站工具指北网站建设 犀牛
  • 房地产行业网站开发展示用网站
  • 企业服务网站怎么免费增加网站流量吗
  • 企业网站建设目的意义南宁网站设计
  • 网站建设自评报告制作公司网站的作用
  • 长沙做网站哪家好wordpress删除主题介绍
  • 免费的行情软件网站入口佛山网站建设开发团队
  • 网站外链代发哪个软件是网页编辑软件
  • 专业网站建设的公司排名微信小程序源码提取工具
  • 管理学习网站北京WordPress爱好者
  • 昆明网站排名优化报价广州住房建设部网站
  • 这是我做的网站吗铁岭手机网站建设
  • 珠海网站建设公司电话网站建设域名费
  • 可以做查询功能的网站做网站搞笑口号