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.

React18のsuspenseとtransitionの組み合わせ

Posted at
App.tsx
import React, {Suspense, useState, useTransition} from 'react';
import {ErrorBoundary} from "react-error-boundary";
import AlbumList from "./AlbumList.tsx";
import Sidebar from "./Sidebar.tsx";
import TodoList from "./TodoList.tsx";

type Tabs = "todo" | "album";

const ReactQuery = () => {
  const [selectedTab, setSelectedTab] = useState<Tabs>("todo");
  const [isPending, startTransition] = useTransition();

  const buttonStyle = {
    padding: "12px",
    fontSize: "16px",
    border: "none",
    opacity: isPending ? 0.5 : 1
  }
  const albumButtonStyle = {
    ...buttonStyle,
    background: selectedTab === "album" ? "royalblue" : "white",
    color: selectedTab === "album" ? "white" : "black"
  }
  const todoButtonStyle = {
    ...buttonStyle,
    background: selectedTab === "todo" ? "royalblue" : "white",
    color: selectedTab === "todo" ? "white" : "black"
  }

  const onClickTabButton = (tab:Tabs) => {
    startTransition(() => {
      setSelectedTab(tab);
    })
  }

  return (
    <>
      <p>ReactQuery</p>

      <div style={{display: "flex", padding: "16px"}}>
        <Sidebar />

        <div style={{flexGrow: 1}}>
          <button style={todoButtonStyle} onClick={() => onClickTabButton("todo")}>todo</button>
          <button style={albumButtonStyle} onClick={() => onClickTabButton("album")}>Album</button>

          <ErrorBoundary fallback={<p>todo or album リストエラーです</p>}>
            <Suspense fallback={<p>todo or albumリストローディング中</p>}>
              {selectedTab === "todo" ? <TodoList /> : <AlbumList />}
            </Suspense>
          </ErrorBoundary>
        </div>
      </div>
    </>
  );
};

export default ReactQuery;

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?