Our team of 体育平台app Web Design gurus are standing by to assist you achieve your online marketing goals.


体育平台app Web Design, LLC aims to improve business by delivering effective solutions based on innovative technologies and professional designs. Discover the variety of services we offer and convince yourself on the basis of the latest works that we've done. We love building fresh, unique and usable websites optimized specifically for your niche.

Responsive Web Design

体育平台app / SEM / Social Media

Conversion Rate Optimization

Email Marketing

Online Presence Analysis

体育平台app Web Design  /  Web Design  /  Get Started With React By Building A Whac-A-Mole Game


About The Author

Jhey makes awesome things for awesome people! He’s a web developer with almost 10 years of experience. Working with and for names such as Eurostar, Uber, … More about Jhey ↬ [1]

Want to get started with React but struggling to find a good place to start? This article should have you covered. We’ll focus on some of the main concepts of React and then we’ll be building a game from scratch! We assume that you have a working knowledge of JavaScript. Ah, and if you’re here for the game, you can get started right away [2] .

I’ve been working with React since ~v0.12 was released. (2014! Wow, where did the time go?) It’s changed a lot. I recall certain “Aha” moments along the way. One thing that’s remained is the mindset for using it. We think about things in a different way as opposed to working with the DOM direct.

For me, my learning style is to get something up and running as fast as I can. Then I explore deeper areas of the docs and everything included whenever necessary. Learn by doing, having fun, and pushing things!


The aim here is to show you enough React to cover some of those “Aha” moments. Leaving you curious enough to dig into things yourself and create your own apps. I recommend checking out the docs [3] for anything you want to dig into. I won’t be duplicating them.

Please note that you can find all examples in CodePen [4] , but you can also jump to my Github repo [5] for a fully working game.

First App

You can bootstrap a React app in various ways. Below is an example:

																			import React from '' import { render } from '' const App = () =>

{`Time: ${}`}

render( , document.getElementById('app')

See the Pen [Your First React App]( by @jh3y [6] .

See the Pen Your First React App [7] by @jh3y [8] .

This is pretty much all you need to create your first React app (besides the HTML) to get started. But, we could make this smaller, like so:


{`Time: ${}`}

, document.getElementById('app'))

In the first version, App is a component, but this example tells React DOM to render an element instead of a component . Elements are the HTML elements we see in both examples. What makes a component, is a function returning those elements

Before we get started with components, what’s the deal with this “HTML in JS”?


That “HTML in JS” is JSX. You can read all about JSX in the React documentation [9] . The gist? A syntax extension to JavaScript that allows us to write HTML in JavaScript. It’s like a templating language with full access to JavaScript powers. It’s actually an abstraction on an underlying API. Why do we use it? For most, it’s easier to follow and comprehend than the equal.

																	React.createElement('h1', null, `Time: ${}`)

The thing to take on board with JSX is that this is how you put things in the DOM 99% of the time with React. And it’s also how we bind event handling a lot of the time. That other 1% is a little out of scope for this article. But, sometimes we want to render elements outside the realms of our React application. We can do this using React DOM’s Portal. We can also get direct access to the DOM within the component lifecycle (coming up).

Attributes in JSX are camelCase. For example, onclick becomes onClick . There are some special cases [10] such as class which becomes className . Also, attributes such as style now accept an Object instead of a string .


Note : You can check out all the differences in attributes here [11] .


How do we get our JSX into the DOM? We need to inject it. In most cases, our apps have a single point of entry. And if we are using React, we use React DOM to insert an element/component at that point. You could use JSX without React though. As we mentioned, it’s a syntax extension . You could change how JSX gets interpreted by Babel and have it pump out something different [12] .

Everything within becomes managed by React. This can yield certain performance benefits when we are modifying the DOM a lot. This is because React makes use of a Virtual DOM. Making DOM updates isn’t slow by any means. But, it’s the impact it has within the browser that can impact performance. Each time we update the DOM, browsers need to calculate the rendering changes that need to take place. That can be expensive. Using the Virtual DOM, these DOM updates get kept in memory and synced with the browser DOM in batches when required.

There’s nothing to stop us from having many apps on a page or having only part of a page managed by React.

See the Pen [Two Apps]( by @jh3y [13] .

See the Pen Two Apps [14] by @jh3y [15] .

Take this example. The same app is rendered twice between some regular HTML. Our React app renders the current time using .

							const App = () =>

{`Time: ${}`}

For this example, we’re rendering the app twice between some regular HTML. We should see the title “Many React Apps” followed by some text, then the first rendering of our app appears followed by some text, and then the second rendering of our app.

For a deeper dive into rendering, check out the docs [16] .

Components && Props

This is one of the biggest parts of React to grok. Components are reusable blocks of UI. But underneath, it’s all functions. Components are functions whose arguments we refer to as props . And we can use those “props” to determine what a component should render. Props are “read-only” and you can pass anything in a prop. Even other components. Anything within the tags of a component we access via a special prop, children .

Components are functions that return elements. If we don’t want to show anything, return null .

We can write components in a variety of ways. But, it’s all the same result.

Use a function:

			function App() { return

{`Time: ${}`}


Use a class:

		class App extends React.Component { render() { return

{`Time: ${}`}

} }

Before the release of hooks(coming up), we used class-based components a lot. We needed them for state and accessing the component API. But, with hooks, the use of class-based components has petered out a bit. In general, we always opt for function-based components now. This has various benefits. For one, it requires less code to achieve the same result. Hooks also make it easier to share and reuse logic between components. Also, classes can be confusing. They need the developer to have an understanding of bindings and context.

We’ll be using function-based and you’ll notice we used a different style for our App component.

	const App = () =>

{`Time: ${}`}

That’s valid. The main thing is that our component returns what we want to render. In this case, a single element that is a h1 displaying the current time. If we don’t need to write return , etc. then don’t. But, it’s all preference. And different projects may adopt different styles.

What if we updated our multi-app example to accept props and we extract the h1 as a component?

		const Message = ({ message }) =>


const App = ({ message }) => render( , document.getElementById('app'))

See the Pen [Our First Component Extraction]( by @jh3y [17] .

See the Pen Our First Component Extraction [18] by @jh3y [19] .

That works and now we can change the message prop on App and we’d get different messages rendered. We could’ve made the component Time . But, creating a Message component implies many opportunities to reuse our component. This is the biggest thing about React. It’s about making decisions around architecture/design.

What if we forget to pass the prop to our component? We could provide a default value. Some ways we could do that.

		const Message = ({message = "You forgot me!"}) =>


Or by specifying defaultProps on our component. We can also provide propTypes [20] which is something I’d recommend having a look at. It provides a way to type check props on our components.

	Message.defaultProps = { message: "You forgot me!" }

We can access props in different ways. We’ve used ES6 conveniences to destructure props. But, our Message component could also look like this and work the same.

	const Message = (props) =>


Props are an object passed to the component. We can read them any way we like.

Our App component could even be this:

	const App = (props) =>

It would yield the same result. We refer to this as “Prop spreading”. It’s better to be explicit with what we pass through though.

We could also pass the message as a child.

	const Message = ({ children }) =>


const App = ({ message }) => {message}

Then we refer to the message via the special children prop.

How about taking it further and doing something like have our App pass a message to a component that is also a prop.

	const Time = ({ children }) =>

{`Time: ${children}`}

const App = ({ message, messageRenderer: Renderer }) => {message} render( , document.getElementById('app'))

See the Pen [Passing Components as Props]( by @jh3y [21] .

See the Pen Passing Components as Props [22] by @jh3y [23] .

In this example, we create two apps and one renders the time and another a message. Note how we rename the messageRenderer prop to Renderer in the destructure? React won’t see anything starting with a lowercase letter as a component. That’s because anything starting in lowercase is seen as an element. It would render it as . We’ll rarely use this pattern but it’s a way to show how anything can be a prop and you can do what you want with it.

One thing to make clear is that anything passed as a prop needs processing by the component. For example, want to pass styles to a component, you need to read them and apply them to whatever is being rendered.

Don’t be afraid to experiment with different things. Try different patterns and practice. The skill of determining what should be a component comes through practice. In some cases, it’s obvious, and in others, you might realize it later and refactor.

A common example would be the layout for an application. Think at a high level what that might look like. A layout with children that comprises of a header, footer, some main content. How might that look? It could look like this.

	const Layout = ({ children }) => (

It’s all about building blocks. Think of it like LEGO for apps.

In fact, one thing I’d advocate is getting familiar with Storybook [24] as soon as possible (I’ll create content on this if people would like to see it). Component-driven development isn’t unique to React, we see it in other frameworks too. Shifting your mindset to think this way will help a lot.

Making Changes

Up until now, we’ve only dealt with static rendering. Nothing changes. The biggest thing to take on board for learning React is how React works. We need to understand that components can have state. And we must understand and respect that state drives everything. Our elements react to state changes. And React will only re-render where necessary.

Data flow is unidirectional too. Like a waterfall, state changes flow down the UI hierarchy. Components don’t care about where the data comes from. For example, a component may want to pass state to a child through props. And that change may trigger an update to the child component. Or, components may choose to manage their own internal state which isn’t shared.

These are all design decisions that get easier the more you work with React. The main thing to remember is how unidirectional this flow is. To trigger changes higher up, it either needs to happen via events or some other means passed by props.

Let’s create an example.

			import React, { useEffect, useRef, useState } from '' import { render } from '' const Time = () => { const [time, setTime] = useState( const timer = useRef(null) useEffect(() => { timer.current = setInterval(() => setTime(, 1000) return () => clearInterval(timer.current) }, []) return

{`Time: ${time}`}

} const App = () =>

See the Pen [An Updating Timer]( by @jh3y [25] .

See the Pen An Updating Timer [26] by @jh3y [27] .

There is a fair bit to digest there. But, here we introduce the use of “Hooks”. We are using “useEffect”, “useRef”, and “useState”. These are utility functions that give us access to the component API.

If you check the example, the time is updating every second or 1000ms . And that’s driven by the fact we update the time which is a piece of state. We are doing this within a setInterval . Note how we don’t change time directly. State variables are treated as immutable. We do it through the setTime method we receive from invoking useState . Every time the state updates, our component re-renders if that state is part of the render. useState always returns a state variable and a way to update that piece of state. The argument passed is the initial value for that piece of state.

We use useEffect to hook into the component lifecycle for events such as state changes. Components mount when they’re inserted into the DOM. And they get unmounted when they’re removed from the DOM. To hook into these lifecycle stages, we use effects. And we can return a function within that effect that will fire when the component gets unmounted. The second parameter of useEffect determines when the effect should run. We refer to it as the dependency array. Any listed items that change will trigger the effect to run. No second parameter means the effect will run on every render. And an empty array means the effect will only run on the first render. This array will usually contain state variables or props.

We are using an effect to both set up and tear down our timer when the component mounts and unmounts.

We use a ref to reference that timer. A ref provides a way to keep a reference to things that don’t trigger rendering. We don’t need to use state for the timer. It doesn’t affect rendering. But, we need to keep a reference to it so we can clear it on unmount.

Want to dig into hooks a bit before moving on? I wrote an article before about them – “ React Hooks in 5 Minutes [28] ”. And there’s also great info in the React docs [29] .

Our Time component has its own internal state that triggers renders. But, what if we wanted to change the interval length? We could manage that from above in our App component.

		const App = () => { const [interval, updateInterval] = useState(1000) return (
			) }

Our new interval value is being stored in the state of App . And it dictates the rate at which the Time component updates.

The Fragment component is a special component we have access to through React . In React , a component must return a single child or null . We can’t return adjacent elements. But, sometimes we don’t want to wrap our content in a div . Fragment s allow us to avoid wrapper elements whilst keeping React happy.

You’ll also notice our first event bind happening there. We use onChange as an attribute of the input to update the interval .

The updated interval is then passed to Time and the change of interval triggers our effect to run. This is because the second parameter of our useEffect hook now contains interval .

	const Time = ({ interval }) => { const [time, setTime] = useState( const timer = useRef(null) useEffect(() => { timer.current = setInterval(() => setTime(, interval) return () => clearInterval(timer.current) }, [interval]) return

{`Time: ${time}`}


Have a play with the demo and see the changes!

See the Pen [Managed Interval]( by @jh3y [30] .

See the Pen Managed Interval [31] by @jh3y [32] .

I recommend visiting the React documentation [33] if you want to dig into some of these concepts more. But, we’ve seen enough React to get started making something fun! Let’s do it!

Whac-A-Mole React Game

Are you ready? We’ll be creating our very own “Whac-A-Mole” with React! This well-known game [34] is basic in theory but throws up some interesting challenges to build. The important part here is how we’re using React. I’ll gloss over applying styles and making it pretty — that’s your job! Although, I’m happy to take any questions on that.

Also, this game will not be “polished”. But, it works. You can go and make it your own! Add your own features, and so on.


Let’s start by thinking about what we’ve got to make, i.e. what components we may need, and so on:

  • Start/Stop Game
  • Timer
  • Keeping Score
  • Layout
  • Mole Component
Whac-A-Mole Sketch
Whac-A-Mole Sketch ( Large preview [35] )

Starting Point

We’ve learned how to make a component and we can roughly gauge what we need.

		import React, { Fragment } from '' import { render } from '' const Moles = ({ children }) =>
const Mole = () => const Timer = () =>
Time: 00:00
const Score = () =>
Score: 0
const Game = () => (


) render( , document.getElementById('app'))


Before we do anything, we need to be able to start and stop the game. Starting the game will trigger elements like the timer and moles to come to life. This is where we can introduce conditional rendering .

								const Game = () => { const [playing, setPlaying] = useState(false) return (
									{!playing &&


} {playing && ( )}
) }

We have a state variable of playing and we use that to render elements that we need. In JSX, we can use a condition with && to render something if the condition is true . Here we say to render the board and its content if we are playing. This also affects the button text where we can use a ternary [36] .

See the Pen [1. Toggle Play State]( by @jh3y [37] .

See the Pen 1. Toggle Play State [38] by @jh3y [39] .


Let’s get the timer running . By default, we’re going to set a time limit of 30000ms , And we can declare this as a constant outside of our React components.

										const TIME_LIMIT = 30000

Declaring constants in one place is a good habit to pick up. Anything that can be used to configure your app can be co-located in one place.

Our Timer component only cares about three things:

  1. The time it’s counting down;
  2. At what interval it’s going to update;
  3. What it does when it ends.

A first attempt might look like this:

								const Timer = ({ time, interval = 1000, onEnd }) => { const [internalTime, setInternalTime] = useState(time) const timerRef = useRef(time) useEffect(() => { if (internalTime === 0 && onEnd) onEnd() }, [internalTime, onEnd]) useEffect(() => { timerRef.current = setInterval( () => setInternalTime(internalTime - interval), interval ) return () => { clearInterval(timerRef.current) } }, []) return
									{`Time: ${internalTime}`}

But, it only updates once?

See the Pen [2. Attempted Timer]( by @jh3y [40] .

See the Pen 2. Attempted Timer [41] by @jh3y [42] .

We’re using the same interval technique we did before. But, the issue is we’re using state in our interval callback . And this is our first “gotcha”. Because we have an empty dependency array for our effect, it only runs once. The closure for setInterval uses the value of internalTime from the first render. This is an interesting problem and makes us think about how we approach things.

Note : I highly recommend reading this article by Dan Abramov [43] that digs into timers and how to get around this problem. It’s a worthwhile read and provides a deeper understanding. One issue is that empty dependency arrays can often introduce bugs in our React code. There’s also an eslint plugin [44] I’d recommend using to help point these out. The React docs also highlight the potential risks [45] of using the empty dependency array.

One way to fix our Timer would be to update the dependency array for the effect. This would mean that our timerRef would get updated every interval. However, it introduces the issue of drifting accuracy.

		useEffect(() => { timerRef.current = setInterval( () => setInternalTime(internalTime - interval), interval ) return () => { clearInterval(timerRef.current) } }, [internalTime, interval])

If you check this demo, it has the same Timer twice with different intervals and logs the drift to the developer console. A smaller interval or longer time equals a bigger drift.

See the Pen [3. Checking Timer Drift]( by @jh3y [46] .

See the Pen 3. Checking Timer Drift [47] by @jh3y [48] .

We can use a ref to solve our problem. We can use it to track the internalTime and avoid running the effect every interval.

	const timeRef = useRef(time) useEffect(() => { timerRef.current = setInterval( () => setInternalTime((timeRef.current -= interval)), interval ) return () => { clearInterval(timerRef.current) } }, [interval])

And this reduces the drift significantly with smaller intervals too. Timers are sort of an edge case. But, it’s a great example to think about how we use hooks in React . It’s an example that’s stuck with me and helped me understand the “Why?”.

Update the render to divide the time by 1000 and append an s and we have a seconds timer.

See the Pen [4. Rudimentary Timer]( by @jh3y [49] .

See the Pen 4. Rudimentary Timer [50] by @jh3y [51] .

This timer is still rudimentary. It will drift over time. For our game, it’ll be fine. If you want to dig into accurate counters , this is a great video on creating accurate timers [52] with JavaScript.


Let’s make it possible to update the score. How do we score? By whacking a mole! In our case, that means clicking a button . For now, let’s give each mole a score of 100 , and we can pass an onWhack callback to our Mole s.

		const MOLE_SCORE = 100 const Mole = ({ onWhack }) => (
		) const Score = ({ value }) =>
{`Score: ${value}`}
const Game = () => { const [playing, setPlaying] = useState(false) const [score, setScore] = useState(0) const onWhack = points => setScore(score + points) return ( {!playing &&


} {playing && setPlaying(false)} /> }
) }

Note how the onWhack callback gets passed to each Mole , and that the callback updates our score state. These updates will trigger a render.

This is a good time to install the React Developer Tools extension [53] in your browser. There is a neat feature that will highlight component renders in the DOM. Open the “Components” tab in Dev Tools and hit the Settings cog. Select “Highlight updates when components render”:

Setting up React DevTools
Setting up React DevTools ( Large preview [54] )

Open the demo at this link [55] and set the extension to highlight renders. Next, you’ll see that the timer renders as time changes , but when we whack a mole, all components re-render.

Loops in JSX

You might be thinking that the way we’re rendering our Mole s is inefficient. And you’d be right to think that! There’s an opportunity for us here to render these in a loop .

With JSX, we tend to use 99% of the time to render a collection of things. For example [56] :

			const USERS = [ { id: 1, name: 'Sally' }, { id: 2, name: 'Jack' }, ] const App = () => (
    {{ id, name }) =>
  • {name}
  • )}

The alternative would be to generate the content in a for loop and then render the return from a function.

	return (

What’s that key attribute for? That helps React determine what changes need to render . If you can use a unique identifier, then do so! As a last resort, use the index of the item in a collection. (Read the docs on lists [57] for more.)

For our example, we don’t have any data to work with. If you need to generate a collection of things, then here’s a trick you can use:

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

This could work for you in some scenarios.

		return (


{playing &&'Ended')}/> {new Array(5).fill().map((_, id) => )} }

Or, if you want a persistent collection, you could use something like uuid :

			import { v4 as uuid } from '' const MOLE_COLLECTION = new Array(5).fill().map(() => uuid()) // In our JSX { => )}

Ending Game

We can only end our game with the Start button. When we do end it, the score remains when we start again. The onEnd for our Timer also does nothing yet.

See the Pen [6. Looping Moles]( by @jh3y [58] .

See the Pen 6. Looping Moles [59] by @jh3y [60] .

What we need is a third state where we aren’t playing but we have finished. In more complex applications, I’d recommend reaching for XState [61] or using reducers [62] . But, for our app, we can introduce a new state variable: finished . When the state is !playing and finished , we can display the score, reset the timer, and give the option to restart.

We need to put our logic caps on now. If we end the game, then instead of toggling playing , we need to also toggle finished . We could create an endGame and startGame function.

	const endGame = () => { setPlaying(false) setFinished(true) } const startGame = () => { setScore(0) setPlaying(true) setFinished(false) }

When we start a game, we reset the score and put the game into the playing state. This triggers the playing UI to render. When we end the game, we set finished to true . (The reason we don’t reset the score is so we can show it as a result.)

And, when our Timer ends, it should invoke that same function.


It can do that within an effect. If the internalTime hits , then unmount and invoke onEnd .

	useEffect(() => { if (internalTime === 0 && onEnd) { onEnd() } }, [internalTime, onEnd])

We can shuffle our UI rendering to render three states:

  • Fresh
  • Playing
  • Finished
		{!playing && !finished &&


} {playing && {new Array(NUMBER_OF_MOLES).fill().map((_, index) => ( ))} } {finished && }

And now we have a functioning game minus moving moles:

See the Pen [7. Ending a Game]( by @jh3y [63] .

See the Pen 7. Ending a Game [64] by @jh3y [65] .

Note how we’ve reused the Score component. Was there an opportunity there to not repeat Score ? Could you put it in its own conditional? Or does it need to appear there in the DOM. This will come down to your design.

Might you end up with a more generic component to cover it? These are the questions to keep asking. The goal is to keep a separation of concerns with your components . But, you also want to keep portability in mind.


Moles are the centerpiece of our game. They don’t care about the rest of the app. But, they’ll give you their score onWhack . This emphasizes portability .

We aren’t digging into styling in this “Guide”, but for our moles, we can create a container with overflow: hidden that our Mole (button) moves in and out of. The default position of our Mole will be out of view:

Mole Design
Mole design ( Large preview [66] )

We’re going to bring in a third-party solution to make our moles bob up and down. This is an example of how to bring in third-party solutions that work with the DOM . In most cases, we use refs to grab DOM elements, and then we use our solution within an effect.

We’re going to use GreenSock(GSAP) [67] to make our moles bob. We won’t dig into the GSAP APIs today, but if you have any questions about what they’re doing, please ask me!

Here’s an updated Mole with GSAP :

	import gsap from '' const Mole = ({ onWhack }) => { const buttonRef = useRef(null) useEffect(() => { gsap.set(buttonRef.current, { yPercent: 100 }), { yPercent: 0, yoyo: true, repeat: -1, }) }, []) return (
) }

We’ve added a wrapper to the button which allows us to show/hide the Mole , and we’ve also given our button a ref . Using an effect, we can create a tween (GSAP animation) that moves the button up and down.

You’ll also notice that we’re using className which is the attribute equal to class in JSX to apply class names. Why don’t we use the className with GSAP? Because if we have many elements with that className , our effect will try to use them all. This is why useRef is a great choice to stick with.

See the Pen [8. Moving Moles]( by @jh3y [68] .

See the Pen 8. Moving Moles [69] by @jh3y [70] .

Awesome, now we have bobbing Mole s, and our game is complete from a functional sense. They all move exactly the same which isn’t ideal. They should operate at different speeds . The points scored should also reduce the longer it takes for a Mole to get whacked.

Our Mole’s internal logic can deal with how scoring and speeds get updated. Passing the initial speed , delay , and points in as props will make for a more flexible component.


Now, for a breakdown of our Mole logic.

Let’s start with how our points will reduce over time. This could be a good candidate for a ref . We have something that doesn’t affect render whose value could get lost in a closure. We create our animation in an effect and it’s never recreated. On each repeat of our animation, we want to decrease the points value by a multiplier. The points value can have a minimum value defined by a pointsMin prop.

	const bobRef = useRef(null) const pointsRef = useRef(points) useEffect(() => { bobRef.current =, { yPercent: -100, duration: speed, yoyo: true, repeat: -1, delay: delay, repeatDelay: delay, onRepeat: () => { pointsRef.current = Math.floor( Math.max(pointsRef.current * POINTS_MULTIPLIER, pointsMin) ) }, }) return () => { bobRef.current.kill() } }, [delay, pointsMin, speed])

We’re also creating a ref to keep a reference for our GSAP animation. We will use this when the Mole gets whacked. Note how we also return a function that kills the animation on unmount. If we don’t kill the animation on unmount, the repeat code will keep firing.

See the Pen [9. Score Reduction]( by @jh3y [71] .

See the Pen 9. Score Reduction [72] by @jh3y [73] .

What will happen when a mole gets whacked? We need a new state for that.

	const [whacked, setWhacked] = useState(false)

And instead of using the onWhack prop in the onClick of our button , we can create a new function whack . This will set whacked to true and call onWhack with the current pointsRef value.

	const whack = () => { setWhacked(true) onWhack(pointsRef.current) } return (

The last thing to do is respond to the whacked state in an effect with useEffect . Using the dependency array , we can make sure we only run the effect when whacked changes. If whacked is true , we reset the points, pause the animation, and animate the Mole underground. Once underground, we wait for a random delay before restarting the animation. The animation will start speedier using timescale and we set whacked back to false .

	useEffect(() => { if (whacked) { pointsRef.current = points bobRef.current.pause(), { yPercent: 100, duration: 0.1, onComplete: () => { gsap.delayedCall(gsap.utils.random(1, 3), () => { setWhacked(false) bobRef.current .restart() .timeScale(bobRef.current.timeScale() * TIME_MULTIPLIER) }) }, }) } }, [whacked])

That gives us:

See the Pen [10. React to Whacks]( by @jh3y [74] .

See the Pen 10. React to Whacks [75] by @jh3y [76] .

The last thing to do is pass props to our Mole instances that will make them behave differently. But, how we generate these props could cause an issue.

{new Array(MOLES).fill().map((_, id) => ( ))}

This would cause an issue because the props would change on every render as we generate the moles. A better solution could be to generate a new Mole array each time we start the game and iterate over that. This way, we can keep the game random without causing issues.

	const generateMoles = () => new Array(MOLES).fill().map(() => ({ speed: gsap.utils.random(0.5, 1), delay: gsap.utils.random(0.5, 4), points: MOLE_SCORE })) // Create state for moles const [moles, setMoles] = useState(generateMoles()) // Update moles on game start const startGame = () => { setScore(0) setMoles(generateMoles()) setPlaying(true) setFinished(false) } // Destructure mole objects as props
{{speed, delay, points}, id) => ( ))}

And here’s the result! I’ve gone ahead and added some styling along with a few varieties of moles for our buttons.

See the Pen [11. Functioning Whac-a-Mole]( by @jh3y [77] .

See the Pen 11. Functioning Whac-a-Mole [78] by @jh3y [79] .

We now have a fully working “Whac-a-Mole” game built in React. It took us less than 200 lines of code . At this stage, you can take it away and make it your own. Style it how you like, add new features, and so on. Or you can stick around and we can put together some extras!

Tracking The Highest Score

We have a working “Whac-A-Mole”, but how can we keep track of our highest achieved score? We could use an effect to write our score to localStorage every time the game ends. But, what if persisting things was a common need. We could create a custom hook called usePersistentState . This could be a wrapper around useState that reads/writes to localStorage .

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

And then we can use that in our game:

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

We use it exactly the same as useState . And we can hook into onWhack to set a new high score during the game when appropriate:

	const endGame = points => { if (score > highScore) setHighScore(score) // play fanfare! }

How might we be able to tell if our game result is a new high score? Another piece of state? Most likely.

See the Pen [12. Tracking High Score]( by @jh3y [80] .

See the Pen 12. Tracking High Score [81] by @jh3y [82] .

Whimsical Touches

At this stage, we’ve covered everything we need to. Even how to make your own custom hook. Feel free to go off and make this your own.

Sticking around? Let’s create another custom hook for adding audio to our game:

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

This is a rudimentary hook implementation for playing audio . We provide an audio src and then we get back the API to play it. We can add noise when we “whac” a mole. Then the decision will be, is this part of Mole ? Is it something we pass to Mole ? Is it something we invoke in onWhack ?

These are the types of decisions that come up in component-driven development. We need to keep portability in mind. Also, what would happen if we wanted to mute the audio? How could we globally do that? It might make more sense as a first approach to control the audio within the Game component:

	// Inside Game const { play: playAudio } = useAudio('/audio/some-audio.mp3') const onWhack = () => { playAudio() setScore(score + points) }

It’s all about design and decisions. If we bring in lots of audio, renaming the play variable could get tedious. Returning an Array from our hook-like useState would allow us to name the variable whatever we want. But, it also might be hard to remember which index of the Array accounts for which API method.

See the Pen [13. Squeaky Moles]( by @jh3y [83] .

See the Pen 13. Squeaky Moles [84] by @jh3y [85] .

That’s It!

More than enough to get you started on your React journey, and we got to make something interesting. We sure did cover a lot:

  • Creating an app,
  • JSX,
  • Components and props,
  • Creating timers,
  • Using refs,
  • Creating custom hooks.

We made a game! And now you can use your new skills to add new features or make it your own.

Where did I take it? At the time of writing, it’s at this stage so far:

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

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

Where To Go Next!

I hope building “Whac-a-Mole” has motivated you to start your React journey. Where next? Well, here are some links to resources to check out if you’re looking to dig in more — some of which are ones I found useful along the way.

Smashing Editorial (vf, il)


  1. ^ More about Jhey ↬ (
  2. ^ get started right away (
  3. ^ checking out the docs (
  4. ^ CodePen (
  5. ^ my Github repo (
  6. ^ @jh3y (
  7. ^ Your First React App (
  8. ^ @jh3y (
  9. ^ JSX in the React documentation (
  10. ^ some special cases (
  11. ^ here (
  12. ^ have it pump out something different (
  13. ^ @jh3y (
  14. ^ Two Apps (
  15. ^ @jh3y (
  16. ^ the docs (
  17. ^ @jh3y (
  18. ^ Our First Component Extraction (
  19. ^ @jh3y (
  20. ^ propTypes (
  21. ^ @jh3y (
  22. ^ Passing Components as Props (
  23. ^ @jh3y (
  24. ^ Storybook (
  25. ^ @jh3y (
  26. ^ An Updating Timer (
  27. ^ @jh3y (
  28. ^ React Hooks in 5 Minutes (
  29. ^ the React docs (
  30. ^ @jh3y (
  31. ^ Managed Interval (
  32. ^ @jh3y (
  33. ^ React documentation (
  34. ^ well-known game (
  35. ^ Large preview (
  36. ^ ternary (
  37. ^ @jh3y (
  38. ^ 1. Toggle Play State (
  39. ^ @jh3y (
  40. ^ @jh3y (
  41. ^ 2. Attempted Timer (
  42. ^ @jh3y (
  43. ^ reading this article by Dan Abramov (
  44. ^ eslint plugin (
  45. ^ highlight the potential risks (
  46. ^ @jh3y (
  47. ^ 3. Checking Timer Drift (
  48. ^ @jh3y (
  49. ^ @jh3y (
  50. ^ 4. Rudimentary Timer (
  51. ^ @jh3y (
  52. ^ great video on creating accurate timers (
  53. ^ React Developer Tools extension (
  54. ^ Large preview (
  55. ^ this link (
  56. ^ For example (
  57. ^ the docs on lists (
  58. ^ @jh3y (
  59. ^ 6. Looping Moles (
  60. ^ @jh3y (
  61. ^ XState (
  62. ^ using reducers (
  63. ^ @jh3y (
  64. ^ 7. Ending a Game (
  65. ^ @jh3y (
  66. ^ Large preview (
  67. ^ GreenSock(GSAP) (
  68. ^ @jh3y (
  69. ^ 8. Moving Moles (
  70. ^ @jh3y (
  71. ^ @jh3y (
  72. ^ 9. Score Reduction (
  73. ^ @jh3y (
  74. ^ @jh3y (
  75. ^ 10. React to Whacks (
  76. ^ @jh3y (
  77. ^ @jh3y (
  78. ^ 11. Functioning Whac-a-Mole (
  79. ^ @jh3y (
  80. ^ @jh3y (
  81. ^ 12. Tracking High Score (
  82. ^ @jh3y (
  83. ^ @jh3y (
  84. ^ 13. Squeaky Moles (
  85. ^ @jh3y (
  86. ^ @jh3y (
  87. ^ Whac-a-Mole w/ React && GSAP (
  88. ^ @jh3y (

Powered by WPeMatico

Smashing Magazine is a website and eBook publisher that offers editorial content and professional resources for web developers and web designers.

英雄联盟竞猜视频比分 江湖电竞(南宁)赛表赛事 安博电竞查询直播网站 英雄联盟竞猜直播比赛网址 熊猫电竞排名今日 泛亚电竞赛事手游(泛亚电竞注册官网v6.3 IOS版)