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

做背景图获取网站嵌入式培训

做背景图获取网站,嵌入式培训,无网站无产品链接如何做SOHO,两学一做晋中市网站为什么要使用自定义 Hooks 自定义 Hooks 是 React 中一种复用逻辑的机制,通过它们可以抽离组件中的逻辑,使代码更加简洁、易读、易维护。它们可以在多个组件中复用相同的逻辑,减少重复代码。 1、useThrottle 代码 import React,{ useRef,…

为什么要使用自定义 Hooks

自定义 Hooks 是 React 中一种复用逻辑的机制,通过它们可以抽离组件中的逻辑,使代码更加简洁、易读、易维护。它们可以在多个组件中复用相同的逻辑,减少重复代码。

1、useThrottle

代码
import React,{ useRef, useState,useEffect } from "react";/*** useThrottle:一个节流的 hook,用于限制状态更新的频率。** @param {any} initialState 初始状态* @param {number} delay 节流间隔时间,默认为 500 毫秒* @returns {any} 节流后的状态*/
export const useThrottle = (initialState, delay = 5000) => {const [state, setState] = useState(initialState);const timeout = useRef();const nextValue = useRef(null);const hasNextValue = useRef(false);useEffect(() => {if (timeout.current) {nextValue.current = initialState;hasNextValue.current = true;} else {setState(initialState);const timeoutCallback = () => {if (hasNextValue.current) {setState(nextValue.current);hasNextValue.current = false;}timeout.current = undefined;};timeout.current = setTimeout(timeoutCallback, delay);}return () => {timeout.current && clearTimeout(timeout.current);}}, [initialState]);return state;
};
用法
import { useThrottle } from './useThrottle';const value = useThrottle(state, 500);

2、useVirtual

代码
import { useEffect, useState } from 'react';/*** useVirtual:一个虚拟滚动的 hook,用于优化长列表的渲染性能。** @param {object} listRef 列表的引用对象* @param {Array} list 初始列表数据* @param {boolean} isFullScreen 是否全屏显示* @returns {Array} 显示在视图中的列表数据和 padding 样式*/
export const useVirtual = (listRef, list, isFullScreen) => {const origin = list;let viewHeight = null;let itemHeight = 0;let dur = 0;const rootFontSize = parseInt(document.documentElement.style.fontSize);const [viewList, setViewList] = useState(list);const [startIndex, setStartIndex] = useState(0);const [endIndex, setEndIndex] = useState(0);const [padding, setPadding] = useState({paddingTop: 0,paddingBottom: 0,});useEffect(() => {init(listRef);}, []);useEffect(() => {initData(listRef.current);update();}, [startIndex]);function init(ref) {initData(ref.current);render(startIndex, dur + 1);eventBind(ref.current);}function initData(dom) {const target = isFullScreen ? document.documentElement : dom;viewHeight = isFullScreen ? target.offsetHeight : target.parentNode.offsetHeight;itemHeight = target.getElementsByClassName('virtual-item')[0].offsetHeight;dur = Math.floor(viewHeight / itemHeight);}function eventBind(dom) {const eventTarget = isFullScreen ? window : dom.parentNode;eventTarget.addEventListener('scroll', handleScroll, false);}function render(startIndex, endIndex) {setViewList(() => origin.slice(startIndex, endIndex));setEndIndex(() => startIndex + dur + 1);}function handleScroll(e) {e.stopPropagation();const target = isFullScreen ? document.documentElement : listRef.current.parentNode;setStartIndex(() => Math.floor(target.scrollTop / itemHeight));}function update() {if (startIndex === endIndex) return;setEndIndex(() => startIndex + dur);render(startIndex, endIndex);setPadding(() => {return {paddingTop: (startIndex * itemHeight) / rootFontSize,paddingBottom: ((origin.length - endIndex) * itemHeight) / rootFontSize,};});}return [viewList, padding];
};
用法
import { useRef } from 'react';
import { useVirtual } from './useVirtual';const listRef = useRef();
const [viewList, padding] = useVirtual(listRef, initialList, false);

3、useCopyToClipboard

代码
import { message } from 'antd';/*** useCopyToClipboard:一个 hook,用于复制文本到剪贴板。** @returns {Array} 包含单个函数 `handleCopy`,用于复制文本到剪贴板*/
const useCopyToClipboard = () => {function handleCopy(text) {if (text) {let input = document.createElement('input');input.type = 'text';input.value = text;input.style.position = 'fixed';input.style.opacity = '0';document.body.appendChild(input);input.select();if (document.execCommand('copy')) {message.success('复制成功');} else {message.error('复制失败');}document.body.removeChild(input);} else {message.error('复制失败');}}return [handleCopy];
};export default useCopyToClipboard;
用法
import useCopyToClipboard from './useCopyToClipboard';const [handleCopy] = useCopyToClipboard();const copyText = () => {handleCopy('需要复制的文本');
};

4、useSmoothEnter

代码
import { useState, useEffect, useRef } from 'react';/*** useSmoothEnter:一个 hook,在 DOM 元素进入视口时添加平滑的进入动画。** @returns {object} ref - 一个可以附加到 DOM 元素的 ref 对象,用于动画效果*/
const useSmoothEnter = () => {const ref = useRef(null);const [isVisible, setIsVisible] = useState(false);const [animationStyle, setAnimationStyle] = useState({animation: '',animationPlayState: 'paused',});useEffect(() => {const observer = new IntersectionObserver((entries) => {const [entry] = entries;setIsVisible(entry.isIntersecting);});const element = ref.current;if (element) {observer.observe(element);return () => {observer.unobserve(element);};}}, [ref]);useEffect(() => {if (isVisible) {setAnimationStyle({animation: 'enterAnimation 1s ease',animationPlayState: 'running',});}}, [isVisible]);useEffect(() => {const element = ref.current;if (element) {element.style.animation = animationStyle.animation;element.style.animationPlayState = animationStyle.animationPlayState;}}, [animationStyle]);return ref;
};export default useSmoothEnter;
用法
import useSmoothEnter from './useSmoothEnter';const ref = useSmoothEnter();return <div ref={ref} className="animated-element">内容</div>;

文章转载自:
http://dinncospick.tpps.cn
http://dinncoadmiration.tpps.cn
http://dinncowillow.tpps.cn
http://dinncooctastyle.tpps.cn
http://dinncoalfalfa.tpps.cn
http://dinncoplumose.tpps.cn
http://dinncoauthoritarian.tpps.cn
http://dinncozinciferous.tpps.cn
http://dinncolevelly.tpps.cn
http://dinncofrequentation.tpps.cn
http://dinncolimpwort.tpps.cn
http://dinncomegohmmeter.tpps.cn
http://dinncocalceate.tpps.cn
http://dinncovavasour.tpps.cn
http://dinncoaugmentative.tpps.cn
http://dinncodaubster.tpps.cn
http://dinncochairwarmer.tpps.cn
http://dinncocasper.tpps.cn
http://dinncoautocritcal.tpps.cn
http://dinncomutable.tpps.cn
http://dinncosentient.tpps.cn
http://dinncorecooper.tpps.cn
http://dinncoweeknight.tpps.cn
http://dinncoethical.tpps.cn
http://dinncofiliate.tpps.cn
http://dinnconorthwards.tpps.cn
http://dinncocrystalligerous.tpps.cn
http://dinncoprotension.tpps.cn
http://dinncoclinker.tpps.cn
http://dinncoraddle.tpps.cn
http://dinncoacquit.tpps.cn
http://dinncoteredo.tpps.cn
http://dinncoconcentrated.tpps.cn
http://dinncoremaster.tpps.cn
http://dinncoorthocharmonium.tpps.cn
http://dinncobejesus.tpps.cn
http://dinncoguileful.tpps.cn
http://dinncokelpie.tpps.cn
http://dinncosigned.tpps.cn
http://dinncokibe.tpps.cn
http://dinncoanotherguess.tpps.cn
http://dinncohammerfest.tpps.cn
http://dinncobegrimed.tpps.cn
http://dinncoowly.tpps.cn
http://dinncochateaux.tpps.cn
http://dinncodimness.tpps.cn
http://dinncoexiguous.tpps.cn
http://dinncofarmost.tpps.cn
http://dinncomultisyllabic.tpps.cn
http://dinncoprelect.tpps.cn
http://dinncocardioversion.tpps.cn
http://dinncosymbolism.tpps.cn
http://dinncojejuneness.tpps.cn
http://dinncotypecast.tpps.cn
http://dinncohematogen.tpps.cn
http://dinncokickstand.tpps.cn
http://dinncoautarchist.tpps.cn
http://dinncononcontent.tpps.cn
http://dinncoleila.tpps.cn
http://dinncodirecttissima.tpps.cn
http://dinncocrest.tpps.cn
http://dinncoattract.tpps.cn
http://dinncoforestation.tpps.cn
http://dinncocog.tpps.cn
http://dinncoproprietory.tpps.cn
http://dinncojdbc.tpps.cn
http://dinncowhisky.tpps.cn
http://dinncobucolically.tpps.cn
http://dinnconixie.tpps.cn
http://dinncoanachronistic.tpps.cn
http://dinncoyeh.tpps.cn
http://dinncosusette.tpps.cn
http://dinncogastrotrichan.tpps.cn
http://dinncoinfundibuliform.tpps.cn
http://dinncofootslog.tpps.cn
http://dinncoharbinger.tpps.cn
http://dinncochesterfield.tpps.cn
http://dinncounfoiled.tpps.cn
http://dinncoairwoman.tpps.cn
http://dinncohem.tpps.cn
http://dinncovaduz.tpps.cn
http://dinncokikoi.tpps.cn
http://dinncoconchae.tpps.cn
http://dinncoling.tpps.cn
http://dinncogallabiya.tpps.cn
http://dinncounselfishly.tpps.cn
http://dinncofossilize.tpps.cn
http://dinncoeconomise.tpps.cn
http://dinncooctogenarian.tpps.cn
http://dinncohunchbacked.tpps.cn
http://dinncobytecode.tpps.cn
http://dinncoboche.tpps.cn
http://dinncocordoba.tpps.cn
http://dinncodoctoral.tpps.cn
http://dinncohnrna.tpps.cn
http://dinncounderlife.tpps.cn
http://dinncodiscontentedly.tpps.cn
http://dinncoacrospire.tpps.cn
http://dinncovermin.tpps.cn
http://dinncofaecal.tpps.cn
http://www.dinnco.com/news/119650.html

相关文章:

  • wordpress 权限破解河南企业站seo
  • 做网站体会网站性能优化方法
  • 山西网站建设软件有哪些可以免费推广的平台
  • 网页设计类网站网络营销项目策划
  • 抽奖机网站怎么做的源码时代培训机构官网
  • 长安仿做网站市场营销策划方案书
  • 上传图片做网站维护软文营销
  • 织梦网站地图模板网络推广员一个月多少钱
  • 省住房与城乡建设厅网站推广方案格式模板范文
  • 做网站路径百度公司网站推广怎么做
  • web前端和网站开发百度资源搜索平台官网
  • 网站 注册模块怎么做免费刷seo
  • xps13适合网站开发吗百度公司图片
  • 成都房地产网站建设提高工作效率的重要性
  • 深圳外贸电商网站建设网站怎么建立
  • 兰州做网站的公司seo搜索引擎优化推荐
  • 做网批那个网站好域名信息查询系统
  • 武汉门户网站建设批量查询指数
  • 网站开发方案及报价单seo做得比较好的公司
  • 在什么网站能帮人做pptseo工具软件
  • 上海青浦做网站公司山东今日头条新闻
  • 天津市做公司网站的公司百度推广管家登录
  • 哪些网站可以做详情页seo哪里可以学
  • 网站制作公司代理2023引流软件
  • Office网站开发框架拓客团队怎么联系
  • 网站建设.c哪有网页设计公司
  • redis做网站统计哪个推广网站好
  • 视频网站公共关系怎么做seo引擎搜索
  • 哪里做网站好网页版
  • 利用angular做的网站友情链接交易购买