自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

前端精神小伙:React Hooks響應(yīng)式布局

開發(fā) 前端
本文除了介紹React Hooks的響應(yīng)式布局實(shí)現(xiàn),還介紹了如何自定義hooks與使用Context上下文,來復(fù)用,以達(dá)到性能最佳優(yōu)化。

前言

現(xiàn)在稍微大型的站點(diǎn)都會(huì)采用H5/PC端 并行,通過nignx獲取瀏覽器的UA信息來切換站點(diǎn)。

但這對(duì)于一些企業(yè)站點(diǎn)或人手不足的小型項(xiàng)目來說,就很難實(shí)現(xiàn)。

通過CSS媒體查詢實(shí)現(xiàn)響應(yīng)式布局,是主流方式。

但是,有時(shí)在 React 程序中,需要根據(jù)屏幕大小有條件地渲染不同的組件(寫媒體查詢太麻煩了,還不如另寫組件),其實(shí)使用React Hooks,可以更靈活實(shí)現(xiàn)。

[[318739]]

本文的實(shí)現(xiàn)來自: 

  1. Developing responsive layouts with React Hooks 

1. 方案一:innerWidth

一個(gè)很簡單粗略的方案,是個(gè)前端都知道: 

  1. const MyComponent = () => {  
  2.   // 當(dāng)前窗口寬度  
  3.   const width = window.innerWidth;  
  4.   // 鄰介值  
  5.   const breakpoint = 620 
  6.   // 寬度小于620時(shí)渲染手機(jī)組件,反之桌面組件  
  7.   return width < breakpoint ? <MobileComponent /> : <DesktopComponent /> 

這個(gè)簡單的解決方案肯定會(huì)起作用。根據(jù)用戶設(shè)備的窗口寬度,我們可以呈現(xiàn)桌面視圖或手機(jī)視圖。

但是,當(dāng)調(diào)整窗口大小時(shí),未解決寬度值的更新問題,可能會(huì)渲染錯(cuò)誤的組件。

[[318740]]

2. 方案二:Hooks+resize

說著也簡單,監(jiān)聽resize事件時(shí),觸發(fā)useEffect改變數(shù)據(jù)。 

  1. const MyComponent = () => { 
  2.    const [width, setWidth] = React.useState(window.innerWidth);  
  3.   const breakpoint = 620 
  4.   React.useEffect(() => {  
  5.     window.addEventListener("resize", () => setWidth(window.innerWidth));  
  6.   }, []);  
  7.   return width < breakpoint ? <MobileComponent /> : <DesktopComponent /> 

但精通Hooks的你,一定知道這里存在內(nèi)存性能消耗問題:resize事件沒移除!

優(yōu)化版本: 

  1. const useViewport = () => {  
  2.   const [width, setWidth] = React.useState(window.innerWidth);  
  3.   React.useEffect(() => {  
  4.     const handleWindowResize = () => setWidth(window.innerWidth);  
  5.     window.addEventListener("resize", handleWindowResize);  
  6.     return () => window.removeEventListener("resize", handleWindowResize);  
  7.   }, []);  
  8.   return { width };  

[[318741]]

3. 方案三:構(gòu)建useViewport

自定義React Hooks,可以將組件/函數(shù)最大程度的復(fù)用。構(gòu)建一個(gè)也很簡單: 

  1. const useViewport = () => {  
  2.   const [width, setWidth] = React.useState(window.innerWidth);  
  3.   React.useEffect(() => {  
  4.     const handleWindowResize = () => setWidth(window.innerWidth);  
  5.     window.addEventListener("resize", handleWindowResize);  
  6.     return () => window.removeEventListener("resize", handleWindowResize);  
  7.   }, []);  
  8.   return { width };  

精簡后的組件代碼: 

  1. const MyComponent = () => {  
  2.   const { width } = useViewport();  
  3.   const breakpoint = 620 
  4.   return width < breakpoint ? <MobileComponent /> : <DesktopComponent /> 

[[318742]]

但是這里還有另一個(gè)性能問題:

響應(yīng)式布局影響的是多個(gè)組件,如果在多處使用useViewport,這將浪費(fèi)性能。

這時(shí)就需要另一個(gè)React親兒子:React Context(上下文) 來幫忙。

4.終極方案:Hooks+Context

我們將創(chuàng)建一個(gè)新的文件viewportContext,在其中可以存儲(chǔ)當(dāng)前視口大小的狀態(tài)以及計(jì)算邏輯。 

  1. const viewportContext = React.createContext({});  
  2. const ViewportProvider = ({ children }) => {  
  3.   // 順帶監(jiān)聽下高度,備用  
  4.   const [width, setWidth] = React.useState(window.innerWidth);  
  5.   const [height, setHeight] = React.useState(window.innerHeight);  
  6.   const handleWindowResize = () => {  
  7.     setWidth(window.innerWidth);  
  8.     setHeight(window.innerHeight);  
  9.   }  
  10.   React.useEffect(() => {  
  11.     window.addEventListener("resize", handleWindowResize);  
  12.     return () => window.removeEventListener("resize", handleWindowResize);  
  13.   }, []);  
  14.   return (  
  15.     <viewportContext.Provider value={{ width, height }}>  
  16.       {children}  
  17.     </viewportContext.Provider>  
  18.   );  
  19. };  
  20. const useViewport = () => {  
  21.   const { width, height } = React.useContext(viewportContext);  
  22.   return { width, height };  

緊接著,你需要在React根節(jié)點(diǎn),確保已經(jīng)包裹住了App: 

  1. const App = () => {  
  2.   return (  
  3.     <ViewportProvider>  
  4.       <AppComponent />  
  5.     </ViewportProvider>  
  6.   );  

在往后的每次useViewport(),其實(shí)都只是共享Hooks。 

  1. const MyComponent = () => {  
  2.   const { width } = useViewport();  
  3.   const breakpoint = 620 
  4.   return width < breakpoint ? <MobileComponent /> : <DesktopComponent /> 

后記

github上面的響應(yīng)式布局hooks,都是大同小異的實(shí)現(xiàn)方式。

本文除了介紹React Hooks的響應(yīng)式布局實(shí)現(xiàn),還介紹了如何自定義hooks與使用Context上下文,來復(fù)用,以達(dá)到性能最佳優(yōu)化。 

 

責(zé)任編輯:龐桂玉 來源: 前端大全
相關(guān)推薦

2023-08-20 12:37:44

前端開發(fā)

2019-08-20 15:16:26

Reacthooks前端

2020-06-29 15:20:31

前端React Hooks面試題

2021-03-18 08:00:55

組件Hooks React

2023-11-06 08:00:00

ReactJavaScript開發(fā)

2016-12-21 14:35:46

響應(yīng)式網(wǎng)頁布局實(shí)現(xiàn)方法原理

2019-03-13 10:10:26

React組件前端

2022-03-31 17:54:29

ReactHooks前端

2021-05-19 14:25:19

前端開發(fā)技術(shù)

2023-06-01 19:19:41

2025-03-13 00:01:00

2022-08-21 09:41:42

ReactVue3前端

2022-06-27 08:21:05

CSS布局

2020-10-28 09:12:48

React架構(gòu)Hooks

2022-07-18 09:01:58

React函數(shù)組件Hooks

2020-09-19 17:46:20

React Hooks開發(fā)函數(shù)

2022-05-30 10:16:52

SSDM.2

2024-06-26 09:51:23

2022-04-16 20:10:00

React Hookfiber框架

2022-03-22 09:09:17

HookReact前端
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)