1
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?

ReactでProviderを利用する

Posted at

はじめに

表題通り、ReactでProviderを利用します。

成果物

スクリーンショット 2023-11-29 11.57.04.png

設計

IMG_0827.jpeg

・親コンポーネント以下では、Tabの情報が更新可能にする
・子コンポーネントのHeaderでは、Tab情報の更新
・子コンポーネントのContentでは、Tab情報を取得し表示を切り替える

ソースコード

App.tsx
import { Content } from "./Content";
import { Header } from "./Header";
import { TabProvider } from "./TabContext";

const App: React.FC = () => {
  return (
    <TabProvider>
      <Header />
      <Content />
    </TabProvider>
  );
};

export default App;

TabContext.tsx
import React, { useState, createContext, useContext, ReactNode } from "react";

export interface TabContextType {
  activeTab: number;
  setActiveTab: (index: number) => void;
}

export const TabContext = createContext<TabContextType | undefined>(undefined);

export interface TabProviderProps {
  children: ReactNode;
}

export const TabProvider: React.FC<TabProviderProps> = ({ children }) => {
  const [activeTab, setActiveTab] = useState(0);

  return (
    <TabContext.Provider value={{ activeTab, setActiveTab }}>
      {children}
    </TabContext.Provider>
  );
};
Header.tsx
import { useContext } from "react";
import { TabContext } from "./TabContext";

const TABS = ["Tab1", "Tab2", "Tab3", "Tab4"];

export const Header: React.FC = () => {
  const context = useContext(TabContext);
  if (!context) {
    throw new Error("Header must be used within a TabProvider");
  }
  const { activeTab, setActiveTab } = context;

  return (
    <div>
      {TABS.map((tab, index) => (
        <button key={index} onClick={() => setActiveTab(index)}>
          {tab}
        </button>
      ))}
    </div>
  );
};

Content.tsx
import { useContext } from "react";
import { TabContext } from "./TabContext";

export const Content: React.FC = () => {
  const context = useContext(TabContext);
  if (!context) {
    throw new Error("Content must be used within a TabProvider");
  }
  const { activeTab } = context;

  return (
    <div>
      {activeTab === 0 && <div>Content for Tab 1</div>}
      {activeTab === 1 && <div>Content for Tab 2</div>}
      {activeTab === 2 && <div>Content for Tab 3</div>}
      {activeTab === 3 && <div>Content for Tab 4</div>}
    </div>
  );
};

1
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
1
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?