How to create a React Checkbox

 by Robin Wieruch
 - Edit this Post

A short React tutorial by example for beginners about using a checkbox in React. First of all, a checkbox is just an HTML input field with the type of checkbox which can be rendered in React's JSX:

const App = () => {
return (
<div>
<input type="checkbox" />
</div>
);
};

What may be missing is an associated label to signal the user what value is changed with this checkbox:

const App = () => {
return (
<div>
<label>
<input type="checkbox" />
My Value
</label>
</div>
);
};

In your browser, this checkbox can already change its checked state by showing either a check mark or nothing. However, this is just the checkbox's internal HTML state which isn't controlled by React yet. Let's change this by transforming this checkbox from being :

const App = () => {
const [checked, setChecked] = React.useState(false);
const handleChange = () => {
setChecked(!checked);
};
return (
<div>
<label>
<input
type="checkbox"
checked={checked}
onChange={handleChange}
/>
My Value
</label>
<p>Is "My Value" checked? {checked.toString()}</p>
</div>
);
};

By using and an , we can keep track of the checkbox's value via . Next we may want to create a Checkbox component which can be used more than once throughout a React project. Therefore, we will extract it as a new and to it:

const App = () => {
const [checked, setChecked] = React.useState(false);
const handleChange = () => {
setChecked(!checked);
};
return (
<div>
<Checkbox
label="My Value"
value={checked}
onChange={handleChange}
/>
<p>Is "My Value" checked? {checked.toString()}</p>
</div>
);
};
const Checkbox = ({ label, value, onChange }) => {
return (
<label>
<input type="checkbox" checked={value} onChange={onChange} />
{label}
</label>
);
};

Our Checkbox component is a now. For example, if you would give your input element some , every Checkbox component which is used in your React project would use the same style.

If you would want to create a checkbox group now, you could just use multiple Checkbox components side by side and maybe style them with some border and some alignment, so that a user perceives them as a group of checkboxes:

const App = () => {
const [checkedOne, setCheckedOne] = React.useState(false);
const [checkedTwo, setCheckedTwo] = React.useState(false);
const handleChangeOne = () => {
setCheckedOne(!checkedOne);
};
const handleChangeTwo = () => {
setCheckedTwo(!checkedTwo);
};
return (
<div>
<Checkbox
label="Value 1"
value={checkedOne}
onChange={handleChangeOne}
/>
<Checkbox
label="Value 2"
value={checkedTwo}
onChange={handleChangeTwo}
/>
</div>
);
};

That's is it for creating a Checkbox component in React. If you are a beginner in React, I hope this tutorial helped you to understand some concepts and patterns!

Keep reading about 

A short React tutorial by example for beginners about creating a dropdown in React. First of all, a dropdown is just an HTML select element which can be rendered in React's JSX: What may be missing is…

A short React tutorial by example for beginners about using a radio button in React. First of all, a radio button is just an HTML input field with the type of radio which can be rendered in React's…

The Road to React

Learn React by building real world applications. No setup configuration. No tooling. Plain React in 200+ pages of learning material. Learn React like 50.000+ readers.

Get it on Amazon.