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

烟台优化网站免费ppt模板下载大全 完整版无需会员

烟台优化网站,免费ppt模板下载大全 完整版无需会员,网站建设销售找客户话术,搭建局域网什么是 ahooks? ahooks 是一个 React Hooks 库,提供了大量实用的自定义 hooks,帮助开发者更高效地构建 React 应用。其中高级类 hooks 是 ahooks 的一个重要分类,专门用于处理一些高级场景,如受控值、事件发射器、性能…

什么是 ahooks?

ahooks 是一个 React Hooks 库,提供了大量实用的自定义 hooks,帮助开发者更高效地构建 React 应用。其中高级类 hooks 是 ahooks 的一个重要分类,专门用于处理一些高级场景,如受控值、事件发射器、性能优化等。

安装 ahooks

npm install ahooks

高级类 hooks 详解

useControllableValue – 受控值

useControllableValue 用于处理受控和非受控组件的值管理。

import React, { useState } from "react";
import { useControllableValue } from "ahooks";
import { Card, Input, Button, Switch } from "antd";const UseControllableValueExample = () => {const [controlled, setControlled] = useState(false);const [value, setValue] = useControllableValue({value: controlled ? "受控值" : undefined,defaultValue: "默认值",onChange: (val) => {console.log("值变化:", val);},});return (<Card title="useControllableValue 受控值"><div style={{ marginBottom: 16 }}><p><strong>当前值:</strong> {value}</p><p><strong>模式:</strong> {controlled ? "受控" : "非受控"}</p></div><div style={{ marginBottom: 16 }}><Inputvalue={value}onChange={(e) => setValue(e.target.value)}placeholder="输入内容"style={{ marginBottom: 8 }}/><Switchchecked={controlled}onChange={setControlled}checkedChildren="受控"unCheckedChildren="非受控"/></div><Button onClick={() => setValue("重置值")}>重置</Button></Card>);
};

useCreation – 创建值

useCreation 用于创建值,类似于 useMemo,但更稳定。

import React, { useState } from "react";
import { useCreation } from "ahooks";
import { Card, Button } from "antd";const UseCreationExample = () => {const [count, setCount] = useState(0);// 使用 useCreation 创建稳定的对象const stableObject = useCreation(() => {return {id: Math.random(),timestamp: Date.now(),};}, []);// 使用 useCreation 创建计算值const expensiveValue = useCreation(() => {console.log("计算昂贵值");return count * 2 + 10;}, [count]);return (<Card title="useCreation 创建值"><div style={{ marginBottom: 16 }}><p><strong>计数:</strong> {count}</p><p><strong>计算值:</strong> {expensiveValue}</p><p><strong>稳定对象 ID:</strong> {stableObject.id}</p><p><strong>稳定对象时间戳:</strong> {stableObject.timestamp}</p></div><Button onClick={() => setCount(count + 1)}>增加计数</Button></Card>);
};

useEventEmitter – 事件发射器

useEventEmitter 用于创建事件发射器,实现组件间通信。

import React, { useRef } from "react";
import { useEventEmitter } from "ahooks";
import { Card, Button, Input } from "antd";const UseEventEmitterExample = () => {const eventEmitter = useEventEmitter();const handleEmit = () => {eventEmitter.emit();};return (<Card title="useEventEmitter 事件发射器"><div style={{ marginBottom: 16 }}><p>点击按钮发射事件,输入框会自动获得焦点</p></div><Button onClick={handleEmit} style={{ marginBottom: 16 }}>发射事件</Button><InputBox eventEmitter={eventEmitter} /></Card>);
};// 输入框组件
const InputBox = ({ eventEmitter }) => {const inputRef = useRef(null);eventEmitter.useSubscription(() => {inputRef.current?.focus();});return (<Inputref={inputRef}placeholder="输入框会自动获得焦点"style={{ width: "100%" }}/>);
};export default UseEventEmitterExample;

useIsomorphicLayoutEffect – 同构布局副作用

useIsomorphicLayoutEffect 在服务端渲染时使用 useEffect,在客户端使用 useLayoutEffect

import React, { useState, useRef } from "react";
import { useIsomorphicLayoutEffect } from "ahooks";
import { Card, Button } from "antd";const UseIsomorphicLayoutEffectExample = () => {const [count, setCount] = useState(0);const ref = useRef(null);useIsomorphicLayoutEffect(() => {if (ref.current) {// 在布局更新前同步执行ref.current.style.backgroundColor =count % 2 === 0 ? "#f0f0f0" : "#e6f7ff";}}, [count]);return (<Card title="useIsomorphicLayoutEffect 同构布局副作用"><div style={{ marginBottom: 16 }}><p><strong>计数:</strong> {count}</p></div><divref={ref}style={{padding: 16,border: "1px solid #d9d9d9",borderRadius: 4,transition: "background-color 0.3s",}}>这个div的背景色会在布局更新前同步改变</div><Button onClick={() => setCount(count + 1)} style={{ marginTop: 16 }}>切换背景色</Button></Card>);
};

useLatest – 最新值

useLatest 返回一个 ref,始终指向最新的值。

import React, { useState, useEffect } from "react";
import { useLatest } from "ahooks";
import { Card, Button } from "antd";const UseLatestExample = () => {const [count, setCount] = useState(0);const latestCount = useLatest(count);useEffect(() => {const timer = setInterval(() => {console.log("当前值:", count);console.log("最新值:", latestCount.current);}, 1000);return () => clearInterval(timer);}, []);return (<Card title="useLatest 最新值"><div style={{ marginBottom: 16 }}><p><strong>当前值:</strong> {count}</p><p><strong>最新值引用:</strong> {latestCount.current}</p><p style={{ fontSize: "12px", color: "#666" }}>每秒在控制台输出当前值和最新值引用</p></div><Button onClick={() => setCount(count + 1)}>增加计数</Button></Card>);
};

useMemoizedFn – 记忆化函数

useMemoizedFn 用于创建记忆化的函数,避免不必要的重新渲染。

import React, { useState } from "react";
import { useMemoizedFn } from "ahooks";
import { Card, Button } from "antd";const UseMemoizedFnExample = () => {const [count, setCount] = useState(0);const [renderCount, setRenderCount] = useState(0);// 使用 useMemoizedFn 创建稳定的函数const handleClick = useMemoizedFn(() => {setCount(count + 1);console.log("点击处理函数执行");});// 每次渲染都会创建新函数const handleClickNormal = () => {setCount(count + 1);console.log("普通函数执行");};// 强制重新渲染const forceRender = () => {setRenderCount(renderCount + 1);};return (<Card title="useMemoizedFn 记忆化函数"><div style={{ marginBottom: 16 }}><p><strong>计数:</strong> {count}</p><p><strong>渲染次数:</strong> {renderCount}</p></div><div style={{ marginBottom: 16 }}><Button onClick={handleClick} style={{ marginRight: 8 }}>记忆化函数</Button><Button onClick={handleClickNormal} style={{ marginRight: 8 }}>普通函数</Button><Button onClick={forceRender}>强制重新渲染</Button></div><p style={{ fontSize: "12px", color: "#666" }}>记忆化函数在重新渲染时保持稳定,普通函数每次都会重新创建</p></Card>);
};

useReactive – 响应式状态

useReactive 用于创建响应式状态对象。

import React from "react";
import { useReactive } from "ahooks";
import { Card, Button, Input } from "antd";const UseReactiveExample = () => {const state = useReactive({user: {name: "张三",age: 25,},count: 0,list: [1, 2, 3],});const handleUpdateName = () => {state.user.name = "李四";};const handleUpdateAge = () => {state.user.age += 1;};const handleAddCount = () => {state.count += 1;};const handleAddToList = () => {state.list.push(state.list.length + 1);};return (<Card title="useReactive 响应式状态"><div style={{ marginBottom: 16 }}><p><strong>用户名:</strong> {state.user.name}</p><p><strong>年龄:</strong> {state.user.age}</p><p><strong>计数:</strong> {state.count}</p><p><strong>列表:</strong> {state.list.join(", ")}</p></div><div style={{ marginBottom: 16 }}><Inputvalue={state.user.name}onChange={(e) => (state.user.name = e.target.value)}placeholder="输入用户名"style={{ marginBottom: 8 }}/></div><div><Button onClick={handleUpdateName} style={{ marginRight: 8 }}>更新姓名</Button><Button onClick={handleUpdateAge} style={{ marginRight: 8 }}>增加年龄</Button><Button onClick={handleAddCount} style={{ marginRight: 8 }}>增加计数</Button><Button onClick={handleAddToList}>添加列表项</Button></div></Card>);
};

高级类 hooks 速查表

Hook 名称用途描述
useControllableValue受控值处理受控和非受控组件的值管理
useCreation创建值创建稳定的值,类似于 useMemo
useEventEmitter事件发射器创建事件发射器,实现组件间通信
useIsomorphicLayoutEffect同构布局副作用在服务端和客户端使用不同的副作用
useLatest最新值返回一个始终指向最新值的 ref
useMemoizedFn记忆化函数创建记忆化的函数,避免不必要的重新渲染
useReactive响应式状态创建响应式状态对象

 React强大且灵活hooks库——ahooks入门实践之高级类hook(advanced)详解 - 高质量源码分享平台-免费下载各类网站源码与模板及前沿动态资讯

http://www.dtcms.com/wzjs/580745.html

相关文章:

  • 上海网站建设公司推荐成都移动seo
  • 学校网站建设全包哪里有网站开发团队
  • 网站建设参考网站的说明中煤建设协会网站
  • 南宁网站建设公司专门做养老院的网站
  • 全国优秀作文网站wordpress文章编译器修改
  • 福建中兴建设有限公司网站做网站编辑累吗
  • 域名就是网站名吗可以做高中题目的网站
  • 免费网站推广方式那样的网站
  • 做企业网站好的光明新区城市建设局网站
  • 江阴高端网站建设深圳市长城建设有限公司网站
  • 网站建设的方法有哪些方面河南建设信息网首页
  • 国外设计网站参考南昌启航科技
  • 人事怎么做招聘网站比对分析十堰网站建设价格
  • 成都模版网站制作做网站 淘宝
  • 无锡手机网站快速搭建一个网站
  • 全椒网站建设网站建设找哪家公司比较好
  • 网站个人和企业有什么区别苏州建设工程招标代理有限公司
  • 自己网站开发网站营销网站建设
  • 桥梁建设杂志有假网站吗网站建设与管理教学视频教程
  • 西班牙网站后缀网站设计优化方案
  • 合肥网站设阿里巴巴国际贸易网站推广工具
  • 机关单位 网站建设方案策划书水墨风格网站源码
  • 做网站找毛叶子歌dede可以做视频网站
  • 自己做网站微商有哪些公司网站建设比较好
  • 网站建设与管理专业介绍html动态页面
  • 怎么用本机ip做网站千锋教育和黑马哪个好
  • 微信公众号微网站怎么做哪里有市场营销培训班
  • 小企业网站建设厂家有哪些宁夏建设监理协会网站
  • 备案不关闭网站怎么样wordpress游客登录
  • 从网络全角度考量_写出建设一个大型电影网站规划方案重庆seo入门教程