Course contentsShow
← React
Lesson 459 of 2,39613. Controlled ComponentsFree lesson

Controlled Radio Buttons

Radio buttons share a name and use checked={state === 'value'} to control which is selected in a group.

Controlled Radio Buttons

You'll learn: How to control groups of radio buttons in React so state determines which option is selected.

The Core Idea

Radio buttons are unique because they work as a group — only one can be selected at a time. In HTML, this is achieved by giving them all the same name attribute. In React's controlled component pattern, you use state to track which value is currently selected, then set each radio button's checked property based on whether the state matches that button's value.

The Pattern

Instead of using value={state} like text inputs, radio buttons use:

  • checked={state === 'specificValue'} — this expression evaluates to true or false
  • onChange={handleChange} — updates the state to the clicked button's value
  • name="groupName" — keeps them in the same group (important for accessibility)

Think of it like this: Each radio button asks "Is the current state my value?" If yes, it shows as checked. When clicked, it tells React "make me the selected one" by updating the state.

const [size, setSize] = useState('medium');

<label>
  <input 
    type="radio" 
    name="size"
    value="small"
    checked={size === 'small'}
    onChange={(e) => setSize(e.target.value)}
  />
  Small
</label>
<label>
  <input 
    type="radio" 
    name="size"
    value="large"
    checked={size === 'large'}
    onChange={(e) => setSize(e.target.value)}
  />
  Large
</label>

The checked prop does the heavy lifting — it's boolean logic that determines which button appears selected.

Key Takeaway: Radio buttons are controlled by checking if state === 'value' for each button, while all share the same onChange handler that updates state to the clicked button's value.