Radix UI Select Value Not Updating — Fix Controlled Select
In this fix, you'll learn how to manage Radix UI Select value state. This matters for select/dropdown controls, and appears in form fields and configuration selectors.
The Problem
The following code demonstrates the typical mistake:
<Select>
<SelectTrigger>
<SelectValue placeholder="Select..." />
</SelectTrigger>
<SelectContent>
<SelectItem value="1">Option 1</SelectItem>
<SelectItem value="2">Option 2</SelectItem>
</SelectContent>
</Select>
// Selected value not displayed — placeholder always shows
The Fix
Here is the corrected version:
const [value, setValue] = useState('1');
<Select value={value} onValueChange={setValue}>
<SelectTrigger className="w-[180px]">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value="1">Option 1</SelectItem>
<SelectItem value="2">Option 2</SelectItem>
</SelectContent>
</Select>
// Selected value displays correctly in the trigger
Prevention
- Use controlled mode with value and onValueChange props on Select.
- The value string must exactly match a SelectItem value prop.
- SelectValue placeholder shows when no value is selected.
- For uncontrolled usage, use defaultValue prop instead of value.
- SelectItem children render as the displayed text for that option.
Common Mistakes with ui select value
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world RADIX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro