前端精神小伙:React Hooks響應(yīng)式布局
前言
現(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)。
本文的實(shí)現(xiàn)來自:
- Developing responsive layouts with React Hooks
1. 方案一:innerWidth
一個(gè)很簡單粗略的方案,是個(gè)前端都知道:
- const MyComponent = () => {
- // 當(dāng)前窗口寬度
- const width = window.innerWidth;
- // 鄰介值
- const breakpoint = 620;
- // 寬度小于620時(shí)渲染手機(jī)組件,反之桌面組件
- return width < breakpoint ? <MobileComponent /> : <DesktopComponent />;
- }
這個(gè)簡單的解決方案肯定會(huì)起作用。根據(jù)用戶設(shè)備的窗口寬度,我們可以呈現(xiàn)桌面視圖或手機(jī)視圖。
但是,當(dāng)調(diào)整窗口大小時(shí),未解決寬度值的更新問題,可能會(huì)渲染錯(cuò)誤的組件。
2. 方案二:Hooks+resize
說著也簡單,監(jiān)聽resize事件時(shí),觸發(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 ? <MobileComponent /> : <DesktopComponent />;
- }
但精通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ù)最大程度的復(fù)用。構(gòu)建一個(gè)也很簡單:
- 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 ? <MobileComponent /> : <DesktopComponent />;
- }
但是這里還有另一個(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ì)算邏輯。
- 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 (
- <viewportContext.Provider value={{ width, height }}>
- {children}
- </viewportContext.Provider>
- );
- };
- const useViewport = () => {
- const { width, height } = React.useContext(viewportContext);
- return { width, height };
- }
緊接著,你需要在React根節(jié)點(diǎn),確保已經(jīng)包裹住了App:
- const App = () => {
- return (
- <ViewportProvider>
- <AppComponent />
- </ViewportProvider>
- );
- }
在往后的每次useViewport(),其實(shí)都只是共享Hooks。
- const MyComponent = () => {
- const { width } = useViewport();
- const breakpoint = 620;
- 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)化。