0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[React] window resizeでリサイズする

Posted at

シンプルに従来の補法を使ってリサイズする方法

この方法は以前まで自分もよく使っていた方法です。
大事なので、useEffectのretrunでクリーンラップすることです。

  const [WindowHState, setWindowHState] = useState<number>()

  const onChangeBody = () => {
    let timeoutId
    if (timeoutId) return
    timeoutId = setTimeout(() => {
      const H = document.getElementById('l_header').clientHeight
      setWindowHState(window.innerHeight - H - 80)
    }, 500)
  }

  useEffect(() => {
    window.addEventListener('resize', onChangeBody)

    return () => window.removeEventListener('resize', onChangeBody)
  }, [])

  useEffect(() => {
    if (ArrayState === true) {
      const H = document.getElementById('l_header').clientHeight
      googlemapsize.current.style.height = WindowHState
        ? WindowHState + 'px'
        : window.innerHeight - H - 80 + 'px'
    }
  }, [ArrayState, WindowHState])

簡単ではありますが、よかったら是非使ってみてください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?