千峰React:外部库引用

flushSync强制刷新

如果不强制刷新是这样:每次count在下一轮才更新

import { useState, useRef } from 'react'
import { flushSync } from 'react-dom'function App() {const [count, setCount] = useState(0)const ref=useRef(null)const handleClick = () => { setCount(count + 1)console.log(ref.current.innerHTML)}return (<div><button onClick={handleClick}></button><div ref={ref}>{count}</div></div>)
}export default App;

引入强制刷新:

import { useState, useRef } from 'react'
import { flushSync } from 'react-dom'function App() {const [count, setCount] = useState(0)const ref=useRef(null)const handleClick = () => { flushSync(() => { setCount(count + 1)console.log(ref.current.innerHTML)})}return (<div><button onClick={handleClick}></button><div ref={ref}>{count}</div></div>)
}export default App;

react自动进行批处理,一次按按钮修改两次count,最终一起渲染,叫自动批处理

import { useState } from 'react'
import { flushSync } from 'react-dom'function App() {const [count, setCount] = useState(0)const [count1, setCount1] = useState(0)const handleClick = () => { flushSync(() => { setCount(count + 1)setCount1(count1 + 1)})console.log('修改了一次')}return (<div><button onClick={handleClick}>按钮</button><div >{count},{count1}</div></div>)
}export default App;

使用error boundary捕获渲染错误

例如在App组件里调用了Head组件,如果Head组件出错的话,正常情况是会影响整个界面的渲染的

我们想要的效果是当Head组件出错时,单独抛出Head组件的问题,并且不影响其他组件执行,需要用到一些第三方库:react-error-boundary

下载

# npm
npm install react-error-boundary# pnpm
pnpm add react-error-boundary# yarn
yarn add react-error-boundary

使用方法

<ErrorBoundary fallback={<div>Something went wrong</div>}><ExampleApplication />
</ErrorBoundary>

如果Head还出错,也不会影响其他组件的渲染,并且打印Head的错误

懒加载

用到的时候再加载,不用的时候不加载,节省性能

//App.jsx
import MyHead from './MyHead.jsx'
function Head() {return (<div><h1>头部</h1></div>)
}function App() {return (<div>{/* <MyHead /> */}</div>)
}export default App;
//MyHead.jsx
console.log('MyHead.jsx is running...');function MyHead() {return (<div><h1>My Head</h1></div>);
}
export default MyHead;

如果引入组件不使用组件,组件还是会加载一遍的

如果在用户没用到这些组件的时候就加载,会大大滴降低性能

import{useState,lazy,Suspense }from 'react'const MyHead = lazy(() => import('./MyHead.jsx'))
//lazy需要接收一个promise对象,import()返回的是一个promise对象,符合lazy的要求,且语句执行到这里时,不加载
//使用时才加载
function App() {const [show,setShow]=useState(false)return (<div><button onClick={() => setShow(true)}>点击我触发MyHead</button><Suspense fallback={<div>loading</div> } >{show&&<MyHead/>}</Suspense ></div>)
}export default App;

这样就只会在触发MyHead按钮的时候显示MyHead组件了

点击按钮如果网速过慢,还可以显示loading的过程:

createPortal渲染Dom的不同部分

作用是将子组件渲染到 DOM 树中的指定节点,而不是父组件的 DOM 结构中

import {createPortal} from 'react-dom'
function App() {return (<div>{createPortal( <p>这是一个段落</p>,document.querySelector('body'))}</div>)
}export default App;

结构如下

可以用createPortal将模态框渲染到 body 节点,避免受到父组件样式的影响。

<Profiler>和ReactDevTools的性能测试

import { useState ,Profiler} from "react"
function Head({count}) {return (<div>
hello Head{count}</div>)
}
function App() {const [count, setCount] = useState(0)const onRender = (id, phase, actualDuration, baseDuration, startTime, commitTime, interactions) => { console.log(id, phase, actualDuration, baseDuration, startTime, commitTime, interactions)}return (<div><button onClick={() => { setCount(count + 1) }}>点击</button>{count}<Profiler id='Head' onRender={onRender}>{/* {需要id属性,确认你要测试的是哪个组件} */}<Head count={count} /></Profiler></div>)
}export default App;

每次 Head 组件渲染时,onRender 回调函数都会被调用,并打印性能数据到控制台。

总之就是用作分析性能的

hydrateRoot水合与服务的API

CSS-in-JS可以在js里写css

下个库

npm install styled-components

使用库

import styled from 'styled-components'
const Div = styled.div`
width:200px;
height:200px;
background-color:red;
`
const Link = styled.a`
text-decoration:underline;
color:red`
function App() {return (<div><Div /><Link>去百度</Link></div>)
}
export default App

给Link里的链接加上伪类选择器

const Link = styled.a`
text-decoration:underline;
color:red
&:hover{}`

鼠标hover上去会变黄色

import styled from 'styled-components'
const Div = styled.div`
width:200px;
height:200px;
background-color:red;
`
const Link = styled.a`
text-decoration:underline;
color:red;
&:hover{
color:yellow;
}`
function App() {return (<div><Div /><Link href='https://www.baidu.com'>去百度</Link></div>)
}
export default App

如果前面不加&,就是给Div的子类添加hover

import styled from 'styled-components'
const Div = styled.div`
width:200px;
height:200px;
background-color:red;p{
color:blue;
}
`
const Link = styled.a`
text-decoration:underline;
color:red;
&:hover{
color:yellow;
}`
function App() {return (<div><Div><p>嘻嘻嘻我是p</p> </Div ><Link href='https://www.baidu.com'>去百度</Link></div>)
}
export default App

控制display属性,使用模板字符串

import { useState } from 'react'
import styled from 'styled-components'
const Div = styled.div`
width:200px;
height:200px;
display:${({show})=>show?'block':'none'};
background-color:red;p{
color:blue;
}
`
const Link = styled.a`
text-decoration:underline;
color:red;
&:hover{
color:yellow;
}`
function App() {const [show,setShow]=useState(true)return (<div><button onClick={() => setShow(!show)}>切换显示</button><Div title='hello world' show={show}><p>嘻嘻嘻我是p</p> </Div ><Link href='https://www.baidu.com' >去百度</Link></div>)
}
export default App

React里使用TailwindCSS

弹幕说这个很好用

使用 Vite 安装 Tailwind CSS - Tailwind CSS

行吧我没学会用就这样吧

喝了小酒我又回来了

//index.css
@tailwind base;
@tailwind components;
@tailwind utilities;
//App.jsx
export default function App() { return (<h1 className="text-3xl font-bold underline">Hello, Vite!</h1>);
}
//vite配置
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'// https://vite.dev/config/
export default defineConfig({plugins: [react(), tailwindcss()],})

react-spring动画库

pmndrs/react-spring: ✌️ A spring physics based React animation library

# Install the entire library
npm install react-spring
# or just install your specific target (recommended)
npm install @react-spring/web

import { useState } from 'react'
import { useSpring, animated } from "@react-spring/web";const FadeIn = ({ isVisible, children }) => {const styles = useSpring({opacity: isVisible ? 1 : 0,y: isVisible ? 0 : 24,})return <animated.div style={styles}>{children}</animated.div>
}
function App() {const [isVisible, setIsVisible] = useState(true)return (<div><button onClick={()=>setIsVisible(!isVisible) }></button><FadeIn isVisible={isVisible}>hello App</FadeIn></div>)
}
export default App;

可以实现淡入淡出的效果

import { useState } from 'react'
import { useSpring, animated } from "@react-spring/web";const FadeIn = ({ isVisible, children }) => {const styles = useSpring({opacity: isVisible ? 1 : 0,y: isVisible ? 0 : 24,})return (<animated.div style={styles}>{children}<animated.span>{ styles.y.to((val) => val.toFixed(0))}</animated.span></animated.div>)
}
function App() {const [isVisible, setIsVisible] = useState(true)return (<div><button onClick={()=>setIsVisible(!isVisible) }></button><FadeIn isVisible={isVisible}>hello App</FadeIn></div>)
}
export default App;

点击以后数字会改变

Ant Design Charts图表库和echarts for react

zustnd 小浣熊

算了加载不出来

蚊子咬学长推了这个echarts for react

import React, { useEffect } from 'react';
import ReactECharts from 'echarts-for-react';const MyChart = () => {// 这里定义图表的 option 配置const getOption = () => ({title: {text: 'ECharts 示例',},tooltip: {},xAxis: {data: ['A', 'B', 'C', 'D', 'E'],},yAxis: {},series: [{name: '销量',type: 'bar',data: [5, 20, 36, 10, 10],},],});// 处理图表准备完毕的回调const onChartReadyCallback = () => {console.log('Chart is ready');};// 处理图表事件const EventsDict = {click: (params) => {console.log('Chart clicked', params);},};return (<div><ReactEChartsoption={getOption()}notMerge={true}lazyUpdate={true}theme="light"onChartReady={onChartReadyCallback}onEvents={EventsDict}opts={{ renderer: 'canvas' }} // 你可以设置更多选项/></div>);
};export default MyChart;

React-BMapGL地图库

React-BMapGL文档

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/72351.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

防火墙旁挂组网双机热备负载均衡

一&#xff0c;二层交换网络&#xff1a; 使用MSTPVRRP组网形式 VLAN 2--->SW3为主,SW4 作为备份 VLAN 3--->SW4为主,SW3 作为备份 MSTP 设计 --->SW3 、 4 、 5 运行 实例 1 &#xff1a; VLAN 2 实例 2 &#xff1a; VLAN 3 SW3 是实例 1 的主根&#xff0c;实…

结合PyMuPDF+pdfplumber,删除PDF指定文本后面的内容

🚀 一、需求场景解析 在日常办公中,我们经常会遇到这样的痛点: 合同处理:收到上百份PDF合同,需要找到"签署页"之后的内容并删除报表加工:批量移除财务报表中的敏感数据区域文档归档:快速提取技术文档的关键章节传统的手动操作方式存在三大致命缺陷: ❗ 耗时…

二、QT和驱动模块实现智能家居----2、编译支持QT的系统

因为我们的Linux内核文件不支持QT系统&#xff08;当然如果你的支持&#xff0c;完全跳过这篇文章&#xff09;&#xff0c;所以我们要从网上下载很多软件包&#xff0c;这里直接用百问网的软件包&#xff0c;非常方便。 一&#xff1a;Ubuntu 配置 1 设置交叉编译工具链 以…

el-select的下拉选择框插入el-checkbox

el-check注意这里要使用model-value绑定数据 <el-selectv-model"selectDevice"multiplecollapse-tags:multiple-limit"5"style"width: 200px"popper-class"select-popover-class" ><el-optionv-for"item in deviceList…

UNION 和 UNION ALL 的区别:深入解析 SQL 中的合并操作

在 SQL 的世界里&#xff0c;当我们需要合并多个查询结果集时&#xff0c;UNION和UNION ALL是两个常用的操作符。虽然它们的功能看起来相似&#xff0c;但实际上有着重要的区别&#xff0c;这些区别在不同的应用场景中会对查询结果和性能产生显著影响。本文将详细探讨UNION和UN…

5.Linux配置虚拟机

步骤一 步骤二 步骤三 步骤四 finalshell

2024华为OD机试真题-热点网站统计(C++)-E卷-100分

2024华为OD机试最新E卷题库-(C卷+D卷+E卷)-(JAVA、Python、C++) 目录 题目描述 输入描述 输出描述 用例1 用例2 考点 题目解析 代码 c++ 题目描述 企业路由器的统计页面,有一个功能需要动态统计公司访问最多的网页 URL top N。 请设计一个算法,可以高效动态统计 …

SOUI基于Zint生成EAN码

EAN码广泛应用与欧洲的零售业。包括EAN-2、EAN-5、EAN-8和EAN-12码。分别编码 2、5、7 或 12 位数字。此外&#xff0c;可以使用 字符将 EAN-2 和 EAN-5 附加符号添加到 EAN-8 和 EAN-13 符号中&#xff0c;就像 UPC 符号一样。 EAN-8校验码计算&#xff1a; 从左往右奇数位的…

QT实现简约美观的动画Checkbox

*最终效果: * 一共三个文件: main.cpp , FancyCheckbox.h , FancyCheckbox.cpp main.cpp #include <QApplication> #include "FancyCheckbox.h" #include <QGridLayout> int main(int argc, char *argv[]) {QApplication a(argc, argv);QWidget* w new…

arm | lrzsz移植记录

1 我的使用场景 开发板无网络, 无奈只得用U盘拷贝文件 文件不大, 每次都插拔U盘, 很繁琐 原来的环境不支持rz等命令 就需要移植这个命令来使用 下载地址 https://ohse.de/uwe/releases/lrzsz-0.12.20.tar.gz 2 编译脚本 # 主要内容在这里 configure_for_arm(){mkdir -p $PA…

Hadoop之01:HDFS分布式文件系统

HDFS分布式文件系统 1.目标 理解分布式思想学会使用HDFS的常用命令掌握如何使用java api操作HDFS能独立描述HDFS三大组件namenode、secondarynamenode、datanode的作用理解并独立描述HDFS读写流程HDFS如何解决大量小文件存储问题 2. HDFS 2.1 HDFS是什么 HDFS是Hadoop中的一…

矩阵 trick 系列 题解

1.AT_dp_r Walk&#xff08;矩阵图论&#xff09; 题意 一个有向图有 n n n 个节点&#xff0c;编号 1 1 1 至 n n n。 给出一个二维数组 A 1... n , 1... n A_{1...n,1...n} A1...n,1...n​&#xff0c;若 A i , j 1 A_{i,j}1 Ai,j​1 说明节点 i i i 到节点 j j j …

使用AoT让.NetFramework4.7.2程序调用.Net8编写的库

1、创建.Net8的库&#xff0c;双击解决方案中的项目&#xff0c;修改如下&#xff0c;启用AoT&#xff1a; <Project Sdk"Microsoft.NET.Sdk"><PropertyGroup><OutputType>Library</OutputType><PublishAot>true</PublishAot>&…

Goby 漏洞安全通告| Ollama /api/tags 未授权访问漏洞(CNVD-2025-04094)

漏洞名称&#xff1a;Ollama /api/tags 未授权访问漏洞&#xff08;CNVD-2025-04094&#xff09; English Name&#xff1a;Ollama /api/tags Unauthorized Access Vulnerability (CNVD-2025-04094) CVSS core: 6.5 风险等级&#xff1a; 中风险 漏洞描述&#xff1a; O…

端到端自动驾驶——cnn网络搭建

论文参考&#xff1a;https://arxiv.org/abs/1604.07316 demo 今天主要来看一个如何通过图像直接到控制的自动驾驶端到端的项目&#xff0c;首先需要配置好我的仿真环境&#xff0c;下载软件udacity&#xff1a; https://d17h27t6h515a5.cloudfront.net/topher/2016/November…

蓝桥杯试题:二分查找

一、问题描述 给定 n 个数形成的一个序列 a&#xff0c;现定义如果一个连续子序列包含序列 a 中所有不同元素&#xff0c;则该连续子序列便为蓝桥序列&#xff0c;现在问你&#xff0c;该蓝桥序列长度最短为多少&#xff1f; 例如 1 2 2 2 3 2 2 1&#xff0c;包含 3 个不同的…

网络空间安全(7)攻防环境搭建

一、搭建前的准备 硬件资源&#xff1a;至少需要两台计算机&#xff0c;一台作为攻击机&#xff0c;用于执行攻击操作&#xff1b;另一台作为靶机&#xff0c;作为被攻击的目标。 软件资源&#xff1a; 操作系统&#xff1a;如Windows、Linux等&#xff0c;用于安装在攻击机和…

DeepSpeek服务器繁忙?这几种替代方案帮你流畅使用!(附本地部署教程)

作者&#xff1a;后端小肥肠 目录 1. 前言 2. 解决方案 2.1. 纳米AI搜索&#xff08;第三方平台&#xff09; 2.2. Github&#xff08;第三方平台&#xff09; 2.3. 硅基流动&#xff08;第三方API&#xff09; 3. 本地部署详细步骤 3.1. 运行配置需求 3.2. 部署教程 4…

prisma+supabase报错无法查询数据

解决方案&#xff0c;在DATABASE_URL后面增加?pgbouncertrue

c语言中return 数字代表的含义

return 数字的含义&#xff1a;表示函数返回一个整数值&#xff0c;通常用于向调用者&#xff08;如操作系统或其他程序&#xff09;传递程序的执行状态或结果。 核心规则&#xff1a; return 0&#xff1a; 含义&#xff1a;表示程序或函数正常结束。 示例&#xff1a; int m…