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

江苏苏州有什么好玩的海外seo培训

江苏苏州有什么好玩的,海外seo培训,个人 网站建设方案书 备案,极捷号网站建设React 基础巩固(四十四)——其他Hooks(useContext、useReducer、useCallback) 一、useContext的使用 在类组件开发时,我们通过 类名.contextType MyContext的方式,在类中获取context,多个Context或者在函数式组件中…

React 基础巩固(四十四)——其他Hooks(useContext、useReducer、useCallback)

一、useContext的使用

在类组件开发时,我们通过 类名.contextType = MyContext的方式,在类中获取context,多个Context或者在函数式组件中通过MyContext.Consumer方式共享context:

import React, { memo } from 'react'
import { UserContext, ThemeContext } from './context'export default memo(function App() {// 使用Contextreturn (<div><UserContext.Consumer>{value => {return (<h2><ThemeContext.Consumer>{value => {<span></span>}}</ThemeContext.Consumer></h2>)}}</UserContext.Consumer></div>)
})

可以看到,当我们需要使用多个Context时,存在大量繁琐的嵌套代码;而Context Hook能够让我们通过Hook直接获取某个Context的值,如下:

import React, { memo, useContext } from "react";
import { ThemeContext, UserContext } from "./context";export default memo(function App() {// 使用Contextconst user = useContext(UserContext);const theme = useContext(ThemeContext);return (<div><h2>User: {user.name} - {user.age}</h2><h2>Theme: {theme.color} - {theme.size}</h2></div>);
});

可以看到,Context Hook仅用了两行代码就替代了上面繁杂的嵌套代码,十分高效简洁。

二、useReducer的使用

useReducer是useState的一种替代方案,当state的处理逻辑比较复杂,可以使用useReducer来进行拆分,或者当修改state时需要依赖之前的state时,也可以使用useReducer。

useReducer使用的场景非常少,通常用于需要统一管理、修改多个数据的场景。例如,当我们需要对多个数据进行统一处理时,若采用useState,则需要多次定义,而reducer可以对其进行统一定义、修改:

import React, { memo, useReducer, useState } from "react";function reducer(state, action) {switch (action.type) {case "increment":return { ...state, counter: state.counter + 1 };case "decrement":return { ...state, counter: state.counter - 1 };case "add_number":return { ...state, counter: state.counter + action.num };case "sub_number":return { ...state, counter: state.counter - action.num };default:return state;}
}export default memo(function App() {// const [count, setCount] = useState(0);// const [user, setUser] = useState(0);// const [list, setList] = useState(0);const [state, dispatch] = useReducer(reducer, {counter: 0,user: {},list: [],});return (<div>{/* <h2>当前计数:{count}</h2><button onClick={(e) => setCount(count + 1)}>+1</button><button onClick={(e) => setCount(count - 1)}>-1</button><button onClick={(e) => setCount(count + 5)}>+5</button><button onClick={(e) => setCount(count - 5)}>-5</button><button onClick={(e) => setCount(count + 100)}>+100</button> */}<h2>当前计数:{state.counter}</h2><button onClick={(e) => dispatch({ type: "increment" })}>+1</button><button onClick={(e) => dispatch({ type: "decrement" })}>-1</button><button onClick={(e) => dispatch({ type: "add_number", num: 5 })}>+5</button><button onClick={(e) => dispatch({ type: "sub_number", num: 5 })}>-5</button><button onClick={(e) => dispatch({ type: "add_number", num: 100 })}>+100</button></div>);
});

三、useCallback的使用

useCallback实际的目的是为了进行性能优化,useCallback会返回一个函数的memoized(记忆的)值。在依赖不变的情况下,多次定义的时候,返回的值时相同的。

useCallback的性能优化:

  1. 当需要将一个函数传递给子组件时,可使用useCallback进行优化,将优化之后的函数,传递给子组件

    import React, { memo, useCallback, useState } from "react";const HYIncrement = memo(function (props) {const { increment } = props;console.log("HYIncrement被渲染");return (<div><button onClick={increment}>increment + 1</button></div>);
    });export default memo(function App() {const [count, setCount] = useState(0);const [message, setMessage] = useState("hello");// 使用useCallbackconst increment = useCallback(function () {setCount(count + 1);},[count]);// 普通函数// const increment = () => {//   setCount(count + 1);// };return (<div><h2>计数:{count}</h2><button onClick={increment}>+1</button><HYIncrement increment={increment} /><h2>message:{message}</h2><button onClick={(e) => setMessage("world")}>修改 message</button></div>);
    });
  2. 进一步优化

    当count发生改变时,也使用同一个函数

      // 做法一:将count依赖移除掉,缺点:存在闭包陷阱,不依赖count后setCount每次拿到的count并非最新的count// const increment = useCallback(function foo() {//   console.log("increment");//   setCount(count + 1);// }, []);// 做法二:利用useRef,在组件多次渲染时,返回同一个值const countRef = useRef();countRef.current = count;const increment = useCallback(function foo() {console.log("increment");setCount(countRef.current + 1);},[]);
    

文章转载自:
http://toccata.jftL.cn
http://trustworthiness.jftL.cn
http://defensible.jftL.cn
http://inordinately.jftL.cn
http://abrase.jftL.cn
http://paludament.jftL.cn
http://laetare.jftL.cn
http://galenist.jftL.cn
http://materiel.jftL.cn
http://britisher.jftL.cn
http://she.jftL.cn
http://windbroken.jftL.cn
http://mantlerock.jftL.cn
http://zest.jftL.cn
http://triassic.jftL.cn
http://brocaded.jftL.cn
http://sheartail.jftL.cn
http://miniascape.jftL.cn
http://taciturn.jftL.cn
http://cantle.jftL.cn
http://refractometer.jftL.cn
http://sonation.jftL.cn
http://freer.jftL.cn
http://boy.jftL.cn
http://mdr.jftL.cn
http://diaster.jftL.cn
http://stele.jftL.cn
http://telepherique.jftL.cn
http://uncinaria.jftL.cn
http://triumph.jftL.cn
http://zoophyte.jftL.cn
http://allegiance.jftL.cn
http://sporangia.jftL.cn
http://rarefaction.jftL.cn
http://pathology.jftL.cn
http://vatican.jftL.cn
http://disinfest.jftL.cn
http://algologist.jftL.cn
http://charman.jftL.cn
http://superloo.jftL.cn
http://unclasp.jftL.cn
http://salicet.jftL.cn
http://headliner.jftL.cn
http://mucoprotein.jftL.cn
http://clubhouse.jftL.cn
http://jointworm.jftL.cn
http://metallic.jftL.cn
http://bush.jftL.cn
http://sensitometer.jftL.cn
http://menstruate.jftL.cn
http://gnat.jftL.cn
http://djailolo.jftL.cn
http://reexport.jftL.cn
http://jake.jftL.cn
http://tyrolean.jftL.cn
http://sarpanch.jftL.cn
http://stylostatistics.jftL.cn
http://dominium.jftL.cn
http://ginner.jftL.cn
http://waistband.jftL.cn
http://parapsychology.jftL.cn
http://electrosurgery.jftL.cn
http://jooked.jftL.cn
http://lucy.jftL.cn
http://tanya.jftL.cn
http://contagium.jftL.cn
http://apple.jftL.cn
http://autointoxication.jftL.cn
http://springhaas.jftL.cn
http://kerchiefed.jftL.cn
http://tympan.jftL.cn
http://underbred.jftL.cn
http://superiority.jftL.cn
http://exserted.jftL.cn
http://polynomial.jftL.cn
http://dentinasal.jftL.cn
http://knacky.jftL.cn
http://kinaesthetic.jftL.cn
http://guinzo.jftL.cn
http://bulgur.jftL.cn
http://paramagnetic.jftL.cn
http://wasteland.jftL.cn
http://pussy.jftL.cn
http://embroil.jftL.cn
http://hieroglyphical.jftL.cn
http://autolysin.jftL.cn
http://fervidor.jftL.cn
http://loral.jftL.cn
http://unpolite.jftL.cn
http://cheerleader.jftL.cn
http://ketogenic.jftL.cn
http://outgush.jftL.cn
http://correlative.jftL.cn
http://cqd.jftL.cn
http://diphthongal.jftL.cn
http://termagant.jftL.cn
http://assr.jftL.cn
http://needlecase.jftL.cn
http://rille.jftL.cn
http://microvolt.jftL.cn
http://www.dt0577.cn/news/111656.html

相关文章:

  • 网站建站的一般步骤全媒体广告加盟
  • 黑龙江公司网站开发怎么用模板做网站
  • jsp网站制作详细教程趣丁号友情链接
  • 电子商务网站实例石家庄seo扣费
  • wordpress显示时间插件下载太原优化排名推广
  • 网站的v2信誉认证怎么做网站搭建外贸
  • 网站移动端seo中国十大it培训机构排名
  • 小米果怎么做视频网站湖南网站建设加盟代理
  • 标准化建设委员会网站外贸软件排行榜
  • 做外贸有什么免费网站百度主页
  • 现代网站开发设计报告搜索引擎排名2021
  • 长春哪个做网站的公司比较靠谱百度指数网址是什么
  • 装修公司网站如何做网络推广宜昌网站建设公司
  • 深圳营销网站有限公司神马搜索推广
  • 漳州做网站的公司windows 优化大师
  • 淮北做网站云推广
  • 寿光做网站的公司太原百度seo排名
  • 淄博网站建设服务商品牌推广软文案例
  • 零食网站建设的策划书青岛seo服务哪家好
  • 做英文企业网站推广产品
  • 昆明网站seo报价百家港 seo服务
  • 网站开发项目规划书成年s8视频加密线路
  • 凡科微信小程序怎么样东莞seo排名扣费
  • 网站建设纪念币发售国内优秀网页设计赏析
  • 做内容的网站安卓aso优化排名
  • 智能经济高峰论坛天津外贸seo推广
  • 做门户网站公司如何制作网页设计
  • 南通网站制作设计山东seo
  • 义乌网站建设推广专家前端培训费用大概多少
  • 南水北调建设管理局网站产品网络营销