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 1 year has passed since last update.

[React]useMemo使ったのに再描画されてる問題

Posted at

背景

パフォーマンス上げるためにcomponentをuseMemoを使ってメモ化したのですが、なぜか再描画されている。
原因はmemo化したcomponentの表示を条件付きレンダーで切り替えていたこと。
簡潔に書くとこんな感じでした。。。

const Parent = () => {
  const child = useMemo(() => <Children />)
  return (
    ...
    {isOpen && (
      <Modal>
        {child}
      </Modal>
    )}
  )
}

解決

js側でレンダリングさせると重くなってしまうのでcssで切り替える。

return (
  ...
  <Modal style={{ display: isOpen ? '' : 'none' }}>
    {child}
  </Modal>
)

ちなみに{visibility: hidden}だとcomponentの階層ごとに消えていくのでめっちゃちらついた

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?