How to create a react memory game

Memory games are a fantastic way to practice your React skills while building something fun! In this tutorial, we’ll guide you through creating your own memory game from scratch using React.

Prerequisites

  • Familiarity with basic React concepts (components, props, state, JSX).
  • Some CSS knowledge for styling (we’ll provide basic examples).

Project Setup

  1. Create a new React project:

    Bash
    npx create-react-app memory-game
    
  2. Structure your project:

    src/
      components/
        Card.js
        GameBoard.js
      App.js
      styles.css
    

Building the Game

Click here for my favorite React reference book 

1. Data and Card Component

  • Card Data Structure (in Card.js)

    JavaScript
    const Card = ({ id, image, isFlipped, isMatched, handleClick }) => {
      return (
        <div 
          className={`card ${isFlipped ? 'flipped' : ''} ${isMatched ? 'matched' : ''}`}
          onClick={() => handleClick(id)}
        >
          <div className="front">{/* Placeholder for a question mark or back image */}</div> 
          <div className="back">
            <img src={image} alt="Card Content" /> 
          </div>
        </div>
      );
    };
    
    export default Card;
    
  • Explanation:

    • Each card has idimage, and flags for isFlipped and isMatched.
    • We use CSS classes for styling the different states of the card.

2. Game Board and Rendering

JavaScript
// In GameBoard.js
import React, { useState, useEffect } from 'react';
import Card from './Card';
import shuffle from 'lodash.shuffle'; // Or your own shuffle function

const GameBoard = () => {
  const [cards, setCards] = useState(shuffleCards()); // We'll define shuffleCards later
  const [flippedCards, setFlippedCards] = useState([]);
  const [matchedCards, setMatchedCards] = useState([]);

  // ... Code for handleClick, checking matches (from previous examples)

  // Function to generate and shuffle card data
  const shuffleCards = () => {
    // ... Add your logic to create card objects with images  
  }

  return (
    <div className="game-board">
      {cards.map((card) => (
        <Card 
          key={card.id} 
          {...card} 
          handleClick={handleClick} 
        />
      ))}
    </div>
  );
};

export default GameBoard;

3. Core Game Logic (Explained in Previous Examples)

  • handleClick (prevents invalid clicks, adds card ID to flippedCards)
  • useEffect (checks for matches, updates matchedCards, resets flippedCards)

4. Styling (styles.css):

CSS
.game-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* Adjust for grid size */
}

.card { 
  /* Add styles for the card container */
}

.flipped {
  /* Styles for when the card is flipped */
}

.matched {
  /* Styles to indicate matched cards */
}

5. Win Condition, Restart, Additional Features (Choose one or two)

Deployment (Briefly mention options)

Conclusion

Congratulations on building your memory game! Try customizing its looks, adding difficulty levels, timers, or other fun features!