UNPKG

vtils

Version:

一个面向业务的 JavaScript/TypeScript 实用程序库。

30 lines 908 B
import { getSystemInfoSync, offWindowResize, onWindowResize } from '@tarojs/taro'; import { useEffect, useState } from 'react'; var getWindowSize = function getWindowSize() { var _getSystemInfoSync = getSystemInfoSync(), width = _getSystemInfoSync.windowWidth, height = _getSystemInfoSync.windowHeight; return { width: width, height: height }; }; export var useWindowSize = function useWindowSize( // 不支持 _initialWidth, // 不支持 _initialHeight) { var _useState = useState(getWindowSize), size = _useState[0], setSize = _useState[1]; useEffect(function () { var getWindowSizeCallback = function getWindowSizeCallback() { setSize(getWindowSize()); }; onWindowResize == null || onWindowResize(getWindowSizeCallback); return function () { offWindowResize == null || offWindowResize(getWindowSizeCallback); }; }, []); return size; };