Open In App

Memory Game from scratch using React

Last Updated : 24 Apr, 2025
Comments
Improve
Suggest changes
3 Likes
Like
Report

In this article, we will create a Memory game using ReactJS. A memory game is a famous game in which a pair of cards are placed in front of a player with their face down. The two pairs are always going to have the same content. The player has the choice to select any two cards at a time and check if the faces of the card match or not. If the face content match then he can keep the images with their face up and continue else the players needs to put them back face down. The game is over when all the pairs have been matched.

Preview of Final Output:

Memory Game Using React

Memory Game Using React

Technologies Used:

Pre-requisites

Approach:

As most of the logic is contained by the GameBoard.js component we will mainly focus on it and break the logic in steps but first Import GameBoard.js into App.js and render it there as App.js is the parent component for the GameBoard.js. Inside the GamBoard functional Component, we will create another function named NewGame() which restarts the whole game by suffling the cards and resetting the other variables. This function will only be executed when the new game button is clicked. Now to handle the clicks on the image card we create a new function called handleSelectedCards() which will simply check which card is selected and store its value in the firstCard and secondCard variables for further checking.

Steps to create the project:

Step 1 : Choose a particular IDE of your choice (preferably VS Code) .

Step 2: Create React Project Folder by using the below commands.

npx create-react-app <<projectname>>

Step 3: Navigate to the project folder.

cd <<projectname>>

Step 4: Create a folder “Game” and add five new files GameBoard.js, Card.js, and Data.js.

Project Structure:

gfg

Project Structure

Example: Write the following code in respective files:

  • App.js:This file imports the GameBoard component and renders it.
  • GameBoard.js: This file contains most of the logic from creation of individual cards to handling the various function calls and representing the cards in specific order.
  • Card.js: This file contains the code to generate individual cards.
  • Data.js:This file contains an array which has data regarding the images like image source, name and individual address it gets imported by GameBoard.js.
  • App.css: This file contains the design for every component.

Javascript

// App.js
// Renders the GameBoardComponent
import "./App.css";
import GameBoard from "./Game/GameBoard";
function App() {
    return (
        <div className="App">
            <GameBoard />
        </div>
    );
}
  
export default App;

                    

Javascript

Javascript

Javascript

CSS

Steps to run the application:

Step 1: Type the following command in the terminal.

npm start

Step 2: Open web browser and type the following URL.

http://localhost:3000/

Output:

GIF  output of memory game

GIF output of memory game



Next Article

Similar Reads