千峰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.pswp.cn/diannao/72351.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/72351.shtml
英文地址,請注明出處:http://en.pswp.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…