ReactJS checked Attribute
Last Updated :
09 Oct, 2024
In ReactJS, managing form elements like checkboxes and radio buttons is a common task, and the checked attribute plays an important role when working with these input elements. The checked attribute is used to control the state of checkboxes and radio buttons in a React component.
This article will explain the role of the checked attribute in React and how to use it with controlled components.
Understanding the checked Attribute in HTML
In traditional HTML, the checked attribute is used to determine whether a checkbox or radio button is selected by default.
Syntax:
<input type="checkbox" checked />
<input type="radio" name="gender" value="male" checked />
- The checkbox will be checked by default.
- The radio button for "male" will be selected by default.
However, in React, handling the checked attribute requires a different approach due to how React manages state and updates the DOM.
How React Handles the checked Attribute?
In React, form elements such as checkboxes and radio buttons can be controlled components, meaning their value (checked or unchecked) is controlled by the component’s state. Instead of relying on the static HTML checked attribute, React uses a combination of the checked property and state to manage the form element’s state dynamically.
In React, you don't simply use checked like in HTML. Instead, you tie the checked property of the input element to the component's state, which reflects whether the checkbox or radio button is selected.
<input type="checkbox" checked={isChecked} onChange={handleChange} />
- checked: This is bound to a piece of state (isChecked) to determine whether the checkbox should be checked.
- onChange: This is an event handler that updates the state when the user interacts with the checkbox.
Steps To Implement ReactJS checked Attribute
Create a React application using the following command.
npx create-react-app foldername
cd foldername
Project Structure
Folder StructureExample 1: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
App.js
import React from 'react';
// Defining our App Component
const App = () => {
return <>
<div>
<h1>GeeksforGeeks</h1>
Input: <input type='checkbox' checked={true} />
</div>
</>;
}
export default App
Run the application using the following command.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Â

Example 2: Now write down the following code in the App.js file. Here, App is our default component where we have written our code.
JavaScript
import React from 'react';
// Defining our App Component
const App = () => {
// Returning our JSX code
return <>
<div>
<h1>GeeksforGeeks</h1>
Input: <input type='radio' checked={true} />
</div>
</>;
}
// Exporting your Default App Component
export default App
Run the application using the following command:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:
Similar Reads
HTML checked Attribute
The checked attribute in HTML is used to indicate whether an element should be checked when the page loads up. It is a Boolean attribute. Note: It can be used with <input> element only where the type is either "checkbox" or "radio". This attribute has been DEPRECATED and is no longer recommen
2 min read
ReactJS htmlFor Attribute
When working with forms in React, you may notice some differences from traditional HTML attributes. One such difference is the htmlFor attribute, which is used instead of the standard for attribute. This article will explain why React uses htmlFor and how to use it effectively when building forms in
3 min read
ReactJS value Attribute
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use value attributes. The value at
2 min read
ReactJS selected Attribute
React.js library is all about splitting the app into several components. Each Component has its own lifecycle. React provides us some in-built methods that we can override at particular stages in the life-cycle of the component. In this article, we will know how to use the selected attribute. The se
1 min read
HTML <input>checked Attribute
The HTML <input checked> attribute is used with checkboxes or radio buttons to specify that the input should be selected (checked) when the page loads. It's a boolean attribute, meaning its presence implies "true."Syntax<input type = "checkbox|radio" checked> Example: In this example, we
2 min read
React MUI Checkbox API
Material-UI is a user interface framework that provides pre-defined and customizable React components for faster and easy web development. The Material-UI components are based on top Material Design by Google. In this article letâs discuss the Checkbox API in the Material-UI library. Checkbox API of
3 min read
ReactJS Blueprint Checkbox Component
BlueprintJS is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex data-dense for desktop applications. Checkbox Component provides a way for users to toggle between checked, unchecked, and indeterminate states. We can use the fol
3 min read
React-Bootstrap Checks Radios
In this article, we'll learn about React-Bootstrap's Checks and Radio components. Checks and Radios, in the context of React-Bootstrap, are specialized components tailored for managing user selections, with a primary focus on checkboxes and radio buttons. Different Types of Checkboxes and Radio Butt
5 min read
React Suite Checkbox Accessibility
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, weâll learn about React suite Checkbox Accessibi
3 min read
React Suite CheckPicker Appearance
React Suite is a front-end library designed for the middle platform and back-end products. React Suite CheckPicker component is used as multiple selectors of data. We can also group data using this component. The appearance prop defines the way how the CheckPicker will visually appear to the users.
3 min read