LoginSignup
0
0

More than 1 year has passed since last update.

React Infinite render loop []!==[]

Last updated at Posted at 2022-11-25

カスタムフックに渡した引数が原因で無限レンダリングが発生した。

const results = useCustomHooks(
  condition === STRING ? [...ARRAY] : [condition],
);

 
[]!==[]
のためレンダリングごとに別の配列が生成される。
useMemoを使うことで解決。

const results = useCustomHooks(
  React.useMemo(() => (condition === STRING ? [...ARRAY] : [condition]), [
    condition,
  ]),
);
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