All files / react useWindowSize.taro.ts

91.67% Statements 11/12
100% Branches 0/0
80% Functions 4/5
91.67% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                1x 1x 1x     1x           1x 1x 1x     1x 1x 1x     1x    
import { useWindowSize as _useWindowSize } from './useWindowSize'
import {
  getSystemInfoSync,
  offWindowResize,
  onWindowResize,
} from '@tarojs/taro'
import { useEffect, useState } from 'react'
 
const getWindowSize = function (): ReturnType<typeof _useWindowSize> {
  const { windowWidth: width, windowHeight: height } = getSystemInfoSync()
  return { width, height }
}
 
export const useWindowSize: typeof _useWindowSize = function (
  // 不支持
  _initialWidth,
  // 不支持
  _initialHeight,
) {
  const [size, setSize] = useState(getWindowSize)
  useEffect(() => {
    const getWindowSizeCallback = () => {
      setSize(getWindowSize())
    }
    onWindowResize?.(getWindowSizeCallback)
    return () => {
      offWindowResize?.(getWindowSizeCallback)
    }
  }, [])
  return size
}