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:
import * as React from 'react';const App = () => {return (<div><input type="checkbox" /></div>);};export default App;
What may be missing is an associated label to signal the user what value is changed with this checkbox:
import * as React from 'react';const App = () => {return (<div><label><input type="checkbox" />My Value</label></div>);};export default App;
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 uncontrolled to controlled:
import * as React from 'react';const App = () => {const [checked, setChecked] = React.useState(false);const handleChange = () => {setChecked(!checked);};return (<div><label><inputtype="checkbox"checked={checked}onChange={handleChange}/>My Value</label><p>Is "My Value" checked? {checked.toString()}</p></div>);};export default App;
By using React's useState Hook and an event handler, we can keep track of the checkbox's value via React's state. 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 function component and pass the necessary props to it:
import * as React from 'react';const App = () => {const [checked, setChecked] = React.useState(false);const handleChange = () => {setChecked(!checked);};return (<div><Checkboxlabel="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>);};export default App;
Our Checkbox component is a reusable component now. For example, if you would give your input element some CSS style in React, 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:
import * as React from 'react';const App = () => {const [checkedOne, setCheckedOne] = React.useState(false);const [checkedTwo, setCheckedTwo] = React.useState(false);const handleChangeOne = () => {setCheckedOne(!checkedOne);};const handleChangeTwo = () => {setCheckedTwo(!checkedTwo);};return (<div><Checkboxlabel="Value 1"value={checkedOne}onChange={handleChangeOne}/><Checkboxlabel="Value 2"value={checkedTwo}onChange={handleChangeTwo}/></div>);};export default App;
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!