React Suspenseのシンプルな形

let userNameGlobal = "";

const getUserName = async () => {
  await new Promise((resolve) => setTimeout(resolve, 4000));

  return "yamada taro";
};

const ChildComponent = () => {
  const func = getUserName().then((data) => {
    userNameGlobal = data;
  });

  if (!userNameGlobal) {
    throw func;
  }
  return <h1>{userNameGlobal}</h1>;
};

jsx

      <Suspense fallback={<h1>Loading...</h1>}>
        <ChildComponent />
      </Suspense>
No.2232
10/31 15:40

edit