新聞中心
前言

專注于為中小企業(yè)提供做網(wǎng)站、網(wǎng)站設計服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)江北免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
現(xiàn)在稍微大型的站點都會采用H5/PC端 并行,通過nignx獲取瀏覽器的UA信息來切換站點。
但這對于一些企業(yè)站點或人手不足的小型項目來說,就很難實現(xiàn)。
通過CSS媒體查詢實現(xiàn)響應式布局,是主流方式。
但是,有時在 React 程序中,需要根據(jù)屏幕大小有條件地渲染不同的組件(寫媒體查詢太麻煩了,還不如另寫組件),其實使用React Hooks,可以更靈活實現(xiàn)。
本文的實現(xiàn)來自:
- Developing responsive layouts with React Hooks
1. 方案一:innerWidth
一個很簡單粗略的方案,是個前端都知道:
- const MyComponent = () => {
- // 當前窗口寬度
- const width = window.innerWidth;
- // 鄰介值
- const breakpoint = 620;
- // 寬度小于620時渲染手機組件,反之桌面組件
- return width < breakpoint ?
: ; - }
這個簡單的解決方案肯定會起作用。根據(jù)用戶設備的窗口寬度,我們可以呈現(xiàn)桌面視圖或手機視圖。
但是,當調(diào)整窗口大小時,未解決寬度值的更新問題,可能會渲染錯誤的組件。
2. 方案二:Hooks+resize
說著也簡單,監(jiān)聽resize事件時,觸發(fā)useEffect改變數(shù)據(jù)。
- const MyComponent = () => {
- const [width, setWidth] = React.useState(window.innerWidth);
- const breakpoint = 620;
- React.useEffect(() => {
- window.addEventListener("resize", () => setWidth(window.innerWidth));
- }, []);
- return width < breakpoint ?
: ; - }
但精通Hooks的你,一定知道這里存在內(nèi)存性能消耗問題:resize事件沒移除!
優(yōu)化版本:
- const useViewport = () => {
- const [width, setWidth] = React.useState(window.innerWidth);
- React.useEffect(() => {
- const handleWindowResize = () => setWidth(window.innerWidth);
- window.addEventListener("resize", handleWindowResize);
- return () => window.removeEventListener("resize", handleWindowResize);
- }, []);
- return { width };
- }
3. 方案三:構(gòu)建useViewport
自定義React Hooks,可以將組件/函數(shù)最大程度的復用。構(gòu)建一個也很簡單:
- const useViewport = () => {
- const [width, setWidth] = React.useState(window.innerWidth);
- React.useEffect(() => {
- const handleWindowResize = () => setWidth(window.innerWidth);
- window.addEventListener("resize", handleWindowResize);
- return () => window.removeEventListener("resize", handleWindowResize);
- }, []);
- return { width };
- }
精簡后的組件代碼:
- const MyComponent = () => {
- const { width } = useViewport();
- const breakpoint = 620;
- return width < breakpoint ?
: ; - }
但是這里還有另一個性能問題:
響應式布局影響的是多個組件,如果在多處使用useViewport,這將浪費性能。
這時就需要另一個React親兒子:React Context(上下文) 來幫忙。
4.終極方案:Hooks+Context
我們將創(chuàng)建一個新的文件viewportContext,在其中可以存儲當前視口大小的狀態(tài)以及計算邏輯。
- const viewportContext = React.createContext({});
- const ViewportProvider = ({ children }) => {
- // 順帶監(jiān)聽下高度,備用
- const [width, setWidth] = React.useState(window.innerWidth);
- const [height, setHeight] = React.useState(window.innerHeight);
- const handleWindowResize = () => {
- setWidth(window.innerWidth);
- setHeight(window.innerHeight);
- }
- React.useEffect(() => {
- window.addEventListener("resize", handleWindowResize);
- return () => window.removeEventListener("resize", handleWindowResize);
- }, []);
- return (
- {children}
- );
- };
- const useViewport = () => {
- const { width, height } = React.useContext(viewportContext);
- return { width, height };
- }
緊接著,你需要在React根節(jié)點,確保已經(jīng)包裹住了App:
- const App = () => {
- return (
- );
- }
在往后的每次useViewport(),其實都只是共享Hooks。
- const MyComponent = () => {
- const { width } = useViewport();
- const breakpoint = 620;
- return width < breakpoint ?
: ; - }
后記
github上面的響應式布局hooks,都是大同小異的實現(xiàn)方式。
本文除了介紹React Hooks的響應式布局實現(xiàn),還介紹了如何自定義hooks與使用Context上下文,來復用,以達到性能最佳優(yōu)化。
當前標題:前端精神小伙:React Hooks響應式布局
網(wǎng)站地址:http://www.dlmjj.cn/article/dpieceg.html


咨詢
建站咨詢
