In this section, we will create a Home page that displays a form to allow users to create a game, invite others or join games. We will also set up navigation between the Home page and the Game page using React Router.
In src/pages create a new folder Home and add two files, index.jsx and home-styles.css
We create a Form component with name and gameID as state values. We create and set a random gameID when this component is mounted on useEffect.
The <input/> is controlled i.e it's value comes from the namestate and we update the namestate on every change. This helps us get the value of the input from our state and is a good way to manage form input elements.
Read more on controlled components
In the Home component, we return the Layout component created earlier, passing the Form as the value of the Content prop and the Image component as the Image prop.
Let's include some styles for the Home page in pages/Home/home-styles.css
We have a list of links, each link has a name ( of the site ) and the url to share through their API. We use .map to convert them into a list of elements <a/>. We pass the shareText and subject which are received as props to this links. The shareText is just a message with an invite link to this game
We also require an image for each link from our assets folder ../../assets/social/${link.name}.png
Let's add the styles in share-buttons/share-buttons.styles.css
We also used a Button component. This is a resusable button extracted from our GameOver component.
Let's create this button component in src/components/button/index.jsx
├───board
├───button -> create this
├───cell
├───gameover
├───layout
├───piece
└───share-buttons
button
├───index.jsx
└───button-styles.css
/src/components/button/index.jsx
import React from'react';
import'./button-styles.css';
constButton=({ onClick, children })=>{
return(
<buttononClick={onClick}className="button">
{children}
</button>
);
};
exportdefault Button;
It receives the onClick event handler as a prop and a childrenprop for the text to show.
Next, let's add routing/navigation to transition from the Home page to the Game page.
For this, let's install React Router, a package which helps us handle client side navigation in React apps.
In your project root folder run:
npm i react-router-dom
In src/App.js create the routes for our App.
/src/App.js
import React from'react';
import{ BrowserRouter as Router, Route }from'react-router-dom';
We import the BrowserRoutercomponent and rename it to Router. This should wrap all our routes.
Next, we import a Route component which we use to specify the component to show for each path in our app.
For the home path / we provide our Home component and this will get displayed. We use the exact prop to ensure that the path name is an exact match in order to render the Home component.
<Routepath="/"exactcomponent={Home}/>
For the route /game we render the Game component.
<Routepath="/game"component={Game}/>
With that set up, here is what our app's home page looks like
If you append /game to the address bar root url, you should see the Game component rendered.
The code for this section can be found in this GitHub branch