When you’re building with the popular front-end library React, leveraging reusable components can make your job much easier. Components like tables and navbars are pretty much essentials—and you can add the ever-useful React checkbox to that list.

What is a React checkbox?

The React checkbox is a React component that implements a checkbox—that is, a widget that either displays a checked (true) or unchecked (false) state. (There’s also first-class support for an indeterminate state in a checkbox.) Checkboxes, alongside switches, are ideal UI components for displaying a boolean state.

With React’s component-based architecture, it’s typical for developers to build a React checkbox component that can be imported across the application. In this article, we’ll discuss how to build a React checkbox, its basic structure, and how to customize it.

The benefits of using React checkboxes

In comparison to an ordinary HTML checkbox, a React checkbox is bound to a React state variable, which makes it easy to connect to the general logic of an application. Additionally, because mounting React components inside one another is made easy by the library, React checkboxes are very portable.

Independently, another benefit is that checkboxes are small and preserve space in the UI. They provide a clear indication of selected options. They may also automatically work with accessibility tools for blind or visually impaired users.

Working with checkboxes in React

Building a React checkbox component is quite straightforward, but still includes all the rudimentary parts of any React component. Let’s go through each of them.

useState Hook

React’s useState hook enables developers to initialize a React state variable within the component’s lifecycle. It also creates a function to safely set that variable. However, we will not include useState inside the React component—unlike Angular or Vue, React has an unidirectional data flow, so we’ll declare that outside of the component and pass it in as a prop. If we initialized the checkbox’s state from within the component, then the state would be inaccessible outside of it! That’s known as an uncontrolled element and isn’t ideal for this scenario.

const Checkbox = (props) => {
	return <></>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
	return <>
		<Checkbox value={val} setValue={setVal}></Checkbox>
	</>
}

This code sets the initial code value of the checkbox to false.

Of course, we could do this over two files and import the Checkbox into the App component—but for the sake of brevity, we’ll keep them in the same file!

Input element

HTML has a native input element with a checkbox type. We’ll build our React checkbox component around this native element, directly interfacing with the HTML element’s native properties.

const Checkbox = (props) => {
	return <input type="checkbox" checked={props.val}/>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
	return <>
		<Checkbox value={val} setValue={setVal}></Checkbox>
	</>
}

We’ll then use the general React event handler, onClick, to alter the checkbox’s state, executed after any clicks. Because checkbox’s only flip state, we can easily set the correct value by inverting the checkbox’s present value.

const Checkbox = (props) => {
	return <input type="checkbox" checked={props.val} onClick={() => {props.setValue(!props.val)}}/>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
	return <>
		<Checkbox value={val} setValue={setVal}></Checkbox>
	</>
}

The core benefit of this approach is it still uses HTML’s native checkbox input element, while simultaneously being apt for React applications.

onChange prop

There is an issue, however, with our current code. Sometimes, a user might change a checkbox’s value without clicking on it. Examples would be using the keyboard to focus on the checkbox and selecting it. Or, by using a screen reader.

Instead of using the onClick prop, we want to use the onChange prop to be more robust.

const Checkbox = (props) => {
	return <input type="checkbox" checked={props.val} onChange={() => {props.setValue(!props.val)}}/>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
	return <>
		<Checkbox value={val} setValue={setVal}></Checkbox>
	</>
}

Adding a checkbox label

Of course, a floating checkbox isn’t too much help to the user. They also need to know what they are checking and un-checking. That’s where a checkbox label comes in handy.

A label is not just a text box that describes the checkbox. It’s also a clickable surface that changes the checkbox value. We can use HTML’s native label element to easily accomplish this. We’ll also set the text inside the label by using a prop so that the value could be set by the parent component!

const Checkbox = (props) => {
	return <label>
		<input type="checkbox" checked={props.val} onChange={() => {props.setValue(!props.val)}}/>
		{props.label}
	</label>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
  const label = "My Checkbox!" 
	return <>
		<Checkbox value={val} setValue={setVal} label={label}></Checkbox>
	</>
}

Form elements

Sometimes, a checkbox might be deployed inside a form. If that’s the case, the checkbox needs a name so that it’s a named input inside of the form block. We can accomplish this by just making name another prop that is passed into the Checkbox component.

const Checkbox = (props) => {
	return <label>
		<input type="checkbox" name={props.name} checked={props.val} onChange={() => {props.setValue(!props.val)}}/>
		{props.label}
	</label>
}
const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
  const label = "My Checkbox!" 
	return <>
		<Checkbox value={val} setValue={setVal} label={label}></Checkbox>
	</>
}

Customizing the checkbox

Of course, almost all React apps are customized with personalized aesthetics. We can apply that same custom styling to our component. While this color scheme won’t necessarily match your application’s, it should illuminate how easy it is to add custom stylings.

Now, let’s break off Checkbox into its own file so that we can isolate our personalizations more cleanly.

// checkbox.js
import React from "react";
import "./checkbox.css";

const Checkbox = (props) => {
  return (
    <label className="checkbox">
      <input
        type="checkbox"
        name={props.name}
        checked={props.val}
        onChange={() => {
          props.setValue(!props.val);
        }}
      />
      {props.label}
    </label>
  );
};

export default Checkbox;

Now we can create a CSS file that will be imported into our component.

/* checkbox.css */

label.checkbox {
	color: #ddd;
	display: flex;
	align-items: center;
	justify-content: left;
    width: 300px;
    gap: 4px;
}

label.checkbox > input {
	outline: none;
	height: 20px;
	border-radius: 5px;
}

Of course, these styles should follow how CSS is configured in an application as well as the application’s overall style guide.

We can now import that Checkbox into the original App.js.

// App.js

import React from "react";

const App = () => {
	const [val, setVal] = useState(false); //initializes checkbox to false 
  const label = "My Checkbox!" 
	return <>
		<Checkbox value={val} setValue={setVal} label={label}></Checkbox>
	</>
}

Additional props

While a standardized checkbox is what’s in scope for today’s tutorial, there are additional optional props you could consider. These include:

  • The ability to personalize the checkbox color or choose between preset color themes
  • A boolean to gray out / disable the checkbox

Of course, it only makes sense to add these features if they are going to be actually invoked in the application.

Closing thoughts

In this article, we explored how to build a configurable and personalized React checkbox component. The component has a configurable label and can dynamically invoke a mutation function. With React’s component-based structure, the component can easily be reused throughout the application.

The best React component library is in Retool. Grab a checkbox component and get building.