Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useContextを介してFishesを取得&管理する #23

Merged
merged 4 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Fishes } from '@/components/Fishes'
import { useLocation } from 'react-router-dom'
import { FishesProvider } from '@/contexts/FishesContext'
import { Top } from '@/pages/Top'
import { Start } from '@/pages/Start'
import { Derick } from '@/pages/Derick'
import { Question } from '@/pages/Question'
import { Answer } from '@/pages/Answer'
import { Derick } from '@/pages/Derick'
import { Fishes } from '@/pages/Fishes'

type State = {
state: {
Expand Down
7 changes: 4 additions & 3 deletions src/contexts/FishesContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// このコードは使われてないけど参考のため残します。
import { createContext } from 'react'
import { Fish } from '@/types/Fish'
import {
Expand All @@ -17,12 +16,14 @@ export const FishesContext = createContext<FishesContextProps>({
})

/**
* FishesをグローバルStateに管理するものと勝手に理解しています。使わない
* FishesをグローバルStateに管理するものと勝手に理解しています。一度ロードするだけで使いまわせる想定
* @param param0 FishesContextProviderProps
* @returns React.Context<FishesContextProps>
*/
export const FishesProvider = ({ children }: FishesContextProviderProps) => {
const { data, isLoading, isError } = useFetch<Array<Fish>>('/api/fishes')
const { data, isLoading, isError } = useFetch<Array<Fish>>(
`${import.meta.env.VITE_API_HOST}/api/fishes`
)

return (
<FishesContext.Provider value={{ data, isLoading, isError }}>
Expand Down
13 changes: 8 additions & 5 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import ReactDOM from 'react-dom/client'
import App from '@/App'
import '@/index.css'
import { MemoryRouter as Router, Routes, Route } from 'react-router-dom'
import { FishesProvider } from '@/contexts/FishesContext'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<Router>
<Routes>
<Route path="/" element={<App />} />
</Routes>
</Router>
<FishesProvider>
<Router>
<Routes>
<Route path="/" element={<App />} />
</Routes>
</Router>
</FishesProvider>
</React.StrictMode>
)
10 changes: 4 additions & 6 deletions src/components/Fishes.tsx → src/pages/Fishes.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { FishList } from '@/components/FishList'
import { Loading } from '@/components/Loading'
import { useFetch } from '@/hooks/useFetch'
import { Fish } from '@/types/Fish'
import { FishesContext } from '@/contexts/FishesContext'
import { useContext } from 'react'
import { Link } from 'react-router-dom'

/**
* お魚を全権取得して一覧表示するコンポーネント。今回のアプリでは使わない。
* お魚を全権取得して一覧表示するコンポーネント。
* @returns FishList | Loading | HTMLDivElement
*/
export const Fishes = () => {
const { data, isLoading, isError } = useFetch<Array<Fish>>(
`${import.meta.env.VITE_API_HOST}/api/fishes`
)
const { data, isLoading, isError } = useContext(FishesContext)

if (isError) {
return <div>エラーが発生しました。。</div>
Expand Down
11 changes: 11 additions & 0 deletions src/pages/Question.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { FishesContext } from '@/contexts/FishesContext'
import { useContext } from 'react'
import { Link } from 'react-router-dom'

export const Question = () => {
const { data } = useContext(FishesContext)
// const { gameMode } = useContext(GameContext) TODO: useContextでゲームモードを管理できるようにする
const gameMode = 'shark'

if (data === undefined) return <div>オフラインのようです。。</div>
const shark = data.filter((n) => n.class === gameMode)
const fish = shark[Math.floor(Math.random() * data.length)]

return (
<div>
<div>Questionコンポーネント</div>
<p>{fish.name_japanese}の英名は?</p>
<Link
to={{
pathname: '/',
Expand Down
1 change: 0 additions & 1 deletion src/types/FishesContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// このコードは使われてないけど参考のため残します。
import { ReactNode } from 'react'
import { Fish } from '@/types/Fish'

Expand Down