Featured image - Crossword puzzle

Creating a Crossword Puzzle game with React.JS – Part 2

In this second part of this tutorial, I will complete the Crossword Puzzle game.

Like in part 1, we will continue to use JSFiddle for testing purposes.  In this part, we will complete this crossword game by handling events for user input.

Learn React online

If you are keen to learn React from the ground-up feel free to check Learn and Understand React JS on Zenva Academy which covers all the basics + lots of bonus topics like React Router and Flux.

Download the source code

You can download all the files associated with this tutorial here

Composing Components

The ability to nest components within other components is a very important feature of React.  Because components should be completely self-contained, any component can be nested inside any other component.

In our game, we have composed quite a few components.  In Game class, we composed the Clues component and in Clues class, we composed the Output component.

We used map, which takes each item of an array, performs some transformation on it, and returns a new array containing the transformed elements.  Testing on JSFiddle, we see that we can get a particular value from an array using the props command.

While this works for single array value, we are hard-coding the Game component.  To handle more than just a set number of clues, we need to change that by mapping each clue to a new Clues component with its content attribute correctly set to the clue value.

So, our code can be one component shorter.


React has a simple, consistent, normalized event system.  Just as React has its own DOM abstract, it also has its own event abstraction.  This is used to normalize the event behavior, as React makes events behave the same across all supported browsers.  React also creates a uniform event system for DOM event objects, replacing them with a wrapper called SyntheticEvent.

DOM events are the events generated by the browser, things like button click events, changing of text inputs, and form submission events.  DOM events are the events that receive a React SyntheticEvent object, which is a normalized event object across all browsers.

Let’s try to handle an input change event in JSFiddle.  This example, has an input text box and what the user types will be echoed into a label.  Much like the behavior in AngularJS.

We initialized the variable value to an empty string.  We included an input textbox which has an onChange attribute as well as a label component that outputs what you typed in the textbox as you type it in.  The onChange event handler, named handleChange, merely sets the state of the value object.

In the puzzle game, I’ve removed the handleChange event handler I’ve had in the input in the first part of this tutorial, and instead, added a Button which has a clickHandler event.

In the clickHandler function, I created some variables to extract the letters from the answer in data.


React has some very basic form support built-in, which includes binding values to the form, binding values back out of the form and accessing individual form elements.  In this Puzzle game, each white box has an input element within them.  The input boxes are there for the input of a character for the crossword.

You will notice there is no value attribute for each <input>.  If the <input> element has value, then it would have been a controlled component.  We are not required to have a controlled component, in this instance.

Let’s take a simple forms input example to test on JSFiddle.

With the value of the input being set, you have controlled this component.  This does not allow you to change the value in the input box at all.  By adding an onChange event handler to the input box, React triggers the onChange event and logs the value shown in the input box as well as whatever character you type.

As you type a character, anywhere in the input box, you will notice that the changed value is being written to the console.  To view the changes in the console, open your Developer Tools in Chrome or Web Console in Firefox.


However, this immediately triggers a re-render for the component which resets the value.  From the user’s point of view, they are typing, but nothing is happening.  The secret to making this work is to populate the input boxes value from the owning component’s state.  This is consistent with the React component design guidance because the value of the input box can change, therefore, it is state.

To allow the user to change the content of the input box we need to update the onChange handle to modify the state.  We also initialize the value in a getInitialState function.

Since we will be rendering our components without a value, this is referred to as uncontrolled components.  Using uncontrolled components tells React to exempt the value of the control from the usual rendering process.  We can still handle the onChange event to detect changes to the input, however.  If you still want to initialize the component with a non-empty value, you can supply a defaultValue prop, which gives the input box a starting value.

Now to complete the onClick event handler.

Finally, we check that the letter the user types in is the correct answer to the clues.

I just demonstrated a basic way of creating a crossword puzzle with React.  There is more that can be done here, like adding dynamic styles if a word is wrong.  What else can you do to improve on this crossword puzzle game?

Published by

Ronica Singh

A software engineer, web/app developer, gamer and love coding for fun. Like to learn a new language or technology. My interests are in game development as well as web app development.

Share this article