46
55

More than 1 year has passed since last update.

【React / Next】SWRのfetch中のチラつきをなくす

Last updated at Posted at 2022-06-30

SWRのfetch中のチラつきをなくすための方法です。
公式に書かれていました。

環境

  • React: 17.0.2
  • Next: 11.1.3
  • SWR: ^1.2.2

サンプルコード

公式から拝借

SWR
const useUser = (id) => {
  const { data, error } = useSWR(`/api/user/${id}`, fetcher)

  return {
    user: data,
    // ここ
    isLoading: !error && !data,
  }
}
component
export const Component = () => {
  const { user, isLoading } = useUser(id);

  if (isLoading) {
    return <Spinner />;
  }

  return (
    // 省略
  );
};

参考

46
55
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
46
55