diff --git a/src/App.js b/src/App.js index e0beefd..bc41f8d 100644 --- a/src/App.js +++ b/src/App.js @@ -1,13 +1,14 @@ -import React, { useState, useRef } from "react"; +import React from "react"; import { useRoutes } from "hookrouter"; import { ThemeProvider } from "theme-ui"; +import { GameProvider } from "./contexts/GameContext"; import theme from "./theme.js"; import Home from "./routes/Home"; import Game from "./routes/Game"; import Join from "./routes/Join"; -const routes = { +const routes = { "/": () => , "/game": () => , "/join": () => @@ -17,7 +18,7 @@ function App() { const route = useRoutes(routes); return ( - {route} + {route} ); } diff --git a/src/contexts/GameContext.js b/src/contexts/GameContext.js new file mode 100644 index 0000000..0dc8548 --- /dev/null +++ b/src/contexts/GameContext.js @@ -0,0 +1,11 @@ +import React, { useState } from "react"; + +const GameContext = React.createContext(); + +export function GameProvider({ children }) { + const [gameId, setGameId] = useState(null); + const value = [gameId, setGameId]; + return {children}; +} + +export default GameContext; diff --git a/src/routes/Game.js b/src/routes/Game.js index 043243a..6de3741 100644 --- a/src/routes/Game.js +++ b/src/routes/Game.js @@ -1,9 +1,14 @@ -import React from "react"; +import React, { useContext } from "react"; import { A } from "hookrouter"; +import GameContext from "../contexts/GameContext"; + function Game() { + const [gameId, setGameId] = useContext(GameContext); + return (
+ {gameId ? `You've joined ${gameId}` : "You've started a new game"} GO TO HOMEGAME!
); diff --git a/src/routes/Home.js b/src/routes/Home.js index 223f9d7..f285058 100644 --- a/src/routes/Home.js +++ b/src/routes/Home.js @@ -1,15 +1,28 @@ -import React from "react"; +import React, { useContext } from "react"; import { navigate } from "hookrouter"; import { Container, Flex, Button } from "theme-ui"; +import GameContext from "../contexts/GameContext"; + function Home() { + const [gameId, setGameId] = useContext(GameContext); + + function handleStartGame() { + setGameId(null); + navigate("/game"); + } + + function handleJoinGame() { + navigate("/join"); + } + return ( - - diff --git a/src/routes/Join.js b/src/routes/Join.js index 7fc3d89..20cb833 100644 --- a/src/routes/Join.js +++ b/src/routes/Join.js @@ -1,12 +1,14 @@ -import React, { useState } from "react"; +import React, { useState, useContext } from "react"; import { navigate } from "hookrouter"; import { Container, Box, Label, Input, Button } from "theme-ui"; +import GameContext from "../contexts/GameContext"; + function Join() { - const [id, setId] = useState(""); + const [gameId, setGameId] = useContext(GameContext); function handleChange(event) { - setId(event.target.value); + setGameId(event.target.value); } function handleSubmit(event) { @@ -18,7 +20,13 @@ function Join() { - +