Creating and joining a Game
Overview
When a player clicks on the Create button in the Home page, we need to direct them to the Game component and forward the name and gameID required by the Game component to create a new game.
Let's handle this in the onSubmit function in the Form component src/pages/Home/index.jsx.
At the top of the file we import the useHistory hook from react-router-dom and use it to create a history object const history = useHistory(). The history object can be used to navigate between pages.
In the handleSubmit function we call history.push which navigates to the page we provide as a parameter. /game?name=${name}&id=${gameID} we navigate to /game and provide 2 query string values name and id which will be accessed by the Game component to create the game.
Invited Players
A player is invited to a game through an invite link like this one
https://<our-app-url>?id=123456.
This is our app url with a query string of the id of the game they have been invited to join.
To handle the case when a player is invited, we need to check if the url already has an id query string, if true, we use that instead of generating a random one in the useEffect in the Form component.
To parse query strings and extract values from them, we'll make use of the query string library
Install this library in the project root directory
Let's make this changes in our Form component
We import the useLocation hook from react-router-dom. It gives us an object that represents the current url.
location.search returns the query strings of the url and we parse that using qs.parse to get the id (renamed to inviteID).
In useEffect, if an inviteID exists, we set it to our gameID state.
if (inviteID) return setGameID(inviteID)
Try entering an id manually e.g http://localhost:3000/?id=12345648. This should be displayed in the form as the id.
Find the code snippet for the complete Home page in this gist
Receiving data in our Game component
In the Game component, we also need to parse the query string to extract the player's name and id and use them to create a game.
Let's add the following to our Game component, the rest of the code remains unchanged.
We get the name and id from the query string and store them in our playerName and gameID refs.
We use the name and id stored in our refs to join the game in socket.emit('join'). In case of an error, we redirect the user back to the Home page / with history.push('/').
We also update the makeMove function to use the correct gameID
To test this out, ensure the web-server is running and create a new game with any player name. View your logs in the browser.
Add multiple players by opening two tabs and using the same id to join the game.
http://localhost:3000/?id=12345648
Get the complete code for the Game component in this gist
The code for this section can be found in this GitHub branch