Get Began With React By Constructing A Whac-A-Mole Sport

No Comments

I’ve been working with React since ~v0.12 was launched. (2014! Wow, the place did the time go?) It’s modified rather a lot. I recall sure “Aha” moments alongside the way in which. One factor that’s remained is the mindset for utilizing it. We take into consideration issues differently versus working with the DOM direct.

For me, my studying fashion is to get one thing up and operating as quick as I can. Then I discover deeper areas of the docs and every part included each time obligatory. Study by doing, having enjoyable, and pushing issues!


The intention right here is to point out you sufficient React to cowl a few of these “Aha” moments. Leaving you curious sufficient to dig into issues your self and create your personal apps.
I like to recommend trying out the docs for something you need to dig into. I gained’t be duplicating them.

Please be aware that you could find all examples in CodePen, however you may as well bounce to my Github repo for a completely working recreation.

First App

You possibly can bootstrap a React app in numerous methods. Under is an instance:

import React from ‘’
import { render } from ‘’

const App = () => <h1>{`Time: ${}`}</h1>

render(<App/>, doc.getElementById(‘app’)

Beginning Level

We’ve realized tips on how to make a part and we are able to roughly gauge what we want.

import React, { Fragment } from ‘’
import { render } from ‘’

const Moles = ({ kids }) => <div>{kids}</div>
const Mole = () => <button>Mole</button>
const Timer = () => <div>Time: 00:00</div>
const Rating = () => <div>Rating: 0</div>

const Sport = () => (

render(<Sport/>, doc.getElementById(‘app’))


Earlier than we do something, we want to have the ability to begin and cease the sport. Beginning the sport will set off components just like the timer and moles to return to life. That is the place we are able to introduce conditional rendering.

const Sport = () => {
const [playing, setPlaying] = useState(false)
return (
{!enjoying && <h1>Whac-A-Mole</h1>}
<button onClick={() => setPlaying(!enjoying)}>
{enjoying ? ‘Cease’ : ‘Begin’}
{enjoying && (
<Rating />
<Timer />
<Mole />
<Mole />
<Mole />
<Mole />
<Mole />

Now we have a state variable of enjoying and we use that to render components that we want. In JSX, we are able to use a situation with && to render one thing if the situation is true. Right here we are saying to render the board and its content material if we’re enjoying. This additionally impacts the button textual content the place we are able to use a ternary.

Open the demo at this hyperlink and set the extension to focus on renders. Subsequent, you’ll see that the timer renders as time modifications, however once we whack a mole, all parts re-render.

Loops in JSX

You may be pondering that the way in which we’re rendering our Moles is inefficient. And also you’d be proper to assume that! There’s a possibility for us right here to render these in a loop.

With JSX, we have a tendency to make use of 99% of the time to render a group of issues. For instance:

const USERS = [
{ id: 1, name: ‘Sally’ },
{ id: 2, name: ‘Jack’ },
const App = () => (
{{ id, title }) => <li key={id}>{title}</li>)}

The choice can be to generate the content material in a for loop after which render the return from a perform.

return (

What’s that key attribute for? That helps React decide what modifications have to render. If you need to use a singular identifier, then achieve this! As a final resort, use the index of the merchandise in a group. (Learn the docs on lists for extra.)

For our instance, we don’t have any knowledge to work with. If you could generate a group of issues, then right here’s a trick you need to use:

new Array(NUMBER_OF_THINGS).fill().map()

This might be just right for you in some eventualities.

return (
<button onClick={() => setPlaying(!enjoying)}>{enjoying ? ‘Cease’ : ‘Begin’}</button>
{enjoying &&
<Rating worth={rating} />
<Timer time={TIME_LIMIT} onEnd={() =>‘Ended’)}/>
{new Array(5).fill().map((_, id) =>
<Mole key={id} onWhack={onWhack} />

Or, if you’d like a persistent assortment, you possibly can use one thing like uuid:

import { v4 as uuid } from ‘’
const MOLE_COLLECTION = new Array(5).fill().map(() => uuid())

// In our JSX
{ =>


Ending Sport

We will solely finish our recreation with the Begin button. Once we do finish it, the rating stays once we begin once more. The onEnd for our Timer additionally does nothing but.

We’re going to herald a third-party resolution to make our moles bob up and down. That is an instance of how to herald third-party options that work with the DOM. Generally, we use refs to seize DOM components, after which we use our resolution inside an impact.

We’re going to make use of GreenSock(GSAP) to make our moles bob. We gained’t dig into the GSAP APIs immediately, however if in case you have any questions on what they’re doing, please ask me!

Right here’s an up to date Mole with GSAP:

import gsap from ‘’

const Mole = ({ onWhack }) => {
const buttonRef = useRef(null)
useEffect(() => {
gsap.set(buttonRef.present, { yPercent: 100 }), {
yPercent: 0,
yoyo: true,
repeat: -1,
}, [])
return (
<div className=”mole-hole”>
onClick={() => onWhack(MOLE_SCORE)}>

We’ve added a wrapper to the button which permits us to point out/cover the Mole, and we’ve additionally given our button a ref. Utilizing an impact, we are able to create a tween (GSAP animation) that strikes the button up and down.

You’ll additionally discover that we’re utilizing className which is the attribute equal to class in JSX to use class names. Why don’t we use the className with GSAP? As a result of if we have now many components with that className, our impact will attempt to use all of them. For this reason useRef is a superb selection to stay with.

See the Pen 8. Transferring Moles by @jh3y.

Superior, now we have now bobbing Moles, and our recreation is full from a practical sense. All of them transfer precisely the identical which isn’t perfect. They need to function at completely different speeds. The factors scored also needs to scale back the longer it takes for a Mole to get whacked.

Our Mole’s inside logic can take care of how scoring and speeds get up to date. Passing the preliminary velocity, delay, and factors in as props will make for a extra versatile part.

<Mole key={index} onWhack={onWhack} factors={MOLE_SCORE} delay={0} velocity={2} />

Now, for a breakdown of our Mole logic.

Let’s begin with how our factors will scale back over time. This could possibly be an excellent candidate for a ref. Now we have one thing that doesn’t have an effect on render whose worth might get misplaced in a closure. We create our animation in an impact and it’s by no means recreated. On every repeat of our animation, we need to lower the factors worth by a multiplier. The factors worth can have a minimal worth outlined by a pointsMin prop.

const bobRef = useRef(null)
const pointsRef = useRef(factors)

useEffect(() => {
bobRef.present =, {
yPercent: -100,
length: velocity,
yoyo: true,
repeat: -1,
delay: delay,
repeatDelay: delay,
onRepeat: () => {
pointsRef.present = Math.flooring(
Math.max(pointsRef.present * POINTS_MULTIPLIER, pointsMin)
return () => {
}, [delay, pointsMin, speed])

We’re additionally making a ref to maintain a reference for our GSAP animation. We’ll use this when the Mole will get whacked. Word how we additionally return a perform that kills the animation on unmount. If we don’t kill the animation on unmount, the repeat code will hold firing.

See the Pen 9. Rating Discount by @jh3y.

What’s going to occur when a mole will get whacked? We’d like a brand new state for that.

const [whacked, setWhacked] = useState(false)

And as a substitute of utilizing the onWhack prop within the onClick of our button, we are able to create a brand new perform whack. This may set whacked to true and name onWhack with the present pointsRef worth.

const whack = () => {

return (
<div className=”mole-hole”>
<button className=”mole” ref={buttonRef} onClick={whack}>

The very last thing to do is reply to the whacked state in an impact with useEffect. Utilizing the dependency array, we are able to ensure we solely run the impact when whacked modifications. If whacked is true, we reset the factors, pause the animation, and animate the Mole underground. As soon as underground, we look ahead to a random delay earlier than restarting the animation. The animation will begin speedier utilizing timescale and we set whacked again to false.

useEffect(() => {
if (whacked) {
pointsRef.present = factors
bobRef.present.pause(), {
yPercent: 100,
length: 0.1,
onComplete: () => {
gsap.delayedCall(gsap.utils.random(1, 3), () => {
.timeScale(bobRef.present.timeScale() * TIME_MULTIPLIER)
}, [whacked])

That provides us:

See the Pen 10. React to Whacks by @jh3y.

The very last thing to do is go props to our Mole cases that can make them behave otherwise. However, how we generate these props might trigger a difficulty.

<div className=”moles”>
{new Array(MOLES).fill().map((_, id) => (
velocity={gsap.utils.random(0.5, 1)}
delay={gsap.utils.random(0.5, 4)}

This could trigger a difficulty as a result of the props would change on each render as we generate the moles. A greater resolution could possibly be to generate a brand new Mole array every time we begin the sport and iterate over that. This manner, we are able to hold the sport random with out inflicting points.

const generateMoles = () => new Array(MOLES).fill().map(() => ({
velocity: gsap.utils.random(0.5, 1),
delay: gsap.utils.random(0.5, 4),
factors: MOLE_SCORE
// Create state for moles
const [moles, setMoles] = useState(generateMoles())
// Replace moles on recreation begin
const startGame = () => {
// Destructure mole objects as props
<div className=”moles”>
{{velocity, delay, factors}, id) => (

And right here’s the consequence! I’ve gone forward and added some styling together with a couple of styles of moles for our buttons.

See the Pen 11. Functioning Whac-a-Mole by @jh3y.

We now have a completely working “Whac-a-Mole” recreation inbuilt React. It took us lower than 200 strains of code. At this stage, you’ll be able to take it away and make it your personal. Fashion it how you want, add new options, and so forth. Or you’ll be able to stick round and we are able to put collectively some extras!

Monitoring The Highest Rating

Now we have a working “Whac-A-Mole”, however how can we hold monitor of our highest achieved rating? We might use an impact to write down our rating to localStorage each time the sport ends. However, what if persisting issues was a typical want. We might create a customized hook referred to as usePersistentState. This could possibly be a wrapper round useState that reads/writes to localStorage.

const usePersistentState = (key, initialValue) => {
const [state, setState] = useState(
? JSON.parse(window.localStorage.getItem(key))
: initialValue
useEffect(() => {
window.localStorage.setItem(key, state)
}, [key, state])
return [state, setState]

After which we are able to use that in our recreation:

const [highScore, setHighScore] = usePersistentState(‘whac-high-score’, 0)

We use it precisely the identical as useState. And we are able to hook into onWhack to set a brand new excessive rating throughout the recreation when acceptable:

const endGame = factors => {
if (rating > highScore) setHighScore(rating) // play fanfare!

How would possibly we be capable to inform if our recreation result’s a brand new excessive rating? One other piece of state? Probably.

See the Pen 12. Monitoring Excessive Rating by @jh3y.

Whimsical Touches

At this stage, we’ve lined every part we have to. Even tips on how to make your personal customized hook. Be at liberty to go off and make this your personal.

Sticking round? Let’s create one other customized hook for including audio to our recreation:

const useAudio = (src, quantity = 1) => {
const [audio, setAudio] = useState(null)
useEffect(() => {
const AUDIO = new Audio(src)
AUDIO.quantity = quantity
}, [src])
return {
play: () =>,
pause: () => audio.pause(),
cease: () => {
audio.currentTime = 0

It is a rudimentary hook implementation for taking part in audio. We offer an audio src after which we get again the API to play it. We will add noise once we “whac” a mole. Then the choice will likely be, is that this a part of Mole? Is it one thing we go to Mole? Is it one thing we invoke in onWhack ?

These are the varieties of selections that come up in component-driven improvement. We have to hold portability in thoughts. Additionally, what would occur if we needed to mute the audio? How might we globally try this? It would make extra sense as a primary method to regulate the audio throughout the Sport part:

// Inside Sport
const { play: playAudio } = useAudio(‘/audio/some-audio.mp3’)
const onWhack = () => {
setScore(rating + factors)

It’s all about design and selections. If we usher in plenty of audio, renaming the play variable might get tedious. Returning an Array from our hook-like useState would enable us to call the variable no matter we wish. However, it additionally may be laborious to recollect which index of the Array accounts for which API methodology.

See the Pen 13. Squeaky Moles by @jh3y.

That’s It!

Greater than sufficient to get you began in your React journey, and we acquired to make one thing attention-grabbing. We certain did cowl rather a lot:

Creating an app,
Elements and props,
Creating timers,
Utilizing refs,
Creating customized hooks.

We made a recreation! And now you need to use your new abilities so as to add new options or make it your personal.

The place did I take it? On the time of writing, it’s at this stage to this point:

See the Pen Whac-a-Mole w/ React && GSAP by @jh3y.

The place To Go Subsequent!

I hope constructing “Whac-a-Mole” has motivated you to begin your React journey. The place subsequent? Properly, listed below are some hyperlinks to assets to take a look at in the event you’re trying to dig in additional — a few of that are ones I discovered helpful alongside the way in which.

React Documentation
Making setInterval Declarative With React Hooks,” Dan Abramov
How To Fetch Information With React Hooks,” Robin Wieruch
When To useMemo And useCallback,” Kent C Dodds
Learn extra React articles proper right here on Smashing Journal

    About Marketing Solution Australia

    We are a digital marketing company with a focus on helping our customers achieve great results across several key areas.

    Request a free quote

    We offer professional SEO services that help websites increase their organic search score drastically in order to compete for the highest rankings even when it comes to highly competitive keywords.

    Subscribe to our newsletter!

    More from our blog

    See all posts

    Leave a Comment