I have a controlled select in React with the values 0, 1, 2, 3, 4, 5, '5+', 'unknown'. When selecting or changing the value I'm getting a string for every value instead of a number for the values 0, 1, 2, 3, 4 and 5.
I was searching on Stackoverflow and found a solution where the value is wrapped with parseInt but since I also have strings as possible values I'm getting 5 for the '5+' string and NaN for 'unknown'.
Someone a working solution?
Form component
...
const { formData, handleInputChange, handleSubmit } = useForm();
const options = [
{ label: 0, value: 0 },
{ label: 1, value: 1 },
{ label: 2, value: 2 },
{ label: 3, value: 3 },
{ label: 4, value: 4 },
{ label: 5, value: 5 },
{ label: '5+', value: '5+' },
{ label: "I don't know", value: 'unknown' },
];
return (
<form onSubmit={handleSubmit(submit)}>
<select id="numberOfElectricityMeters" name="numberOfElectricityMeters" value={formData.numberOfElectricityMeters} onChange={handleInputChange}>
<option value=''>Choose an option</option>
{options.map((option, i) => (
<option key={i} value={option.value}>
{option.label}
</option>
))}
</select>
<select id="numberOfGasMeters" name="numberOfGasMeters" value={formData.numberOfGasMeters} onChange={handleInputChange}>
<option value=''>Choose an option</option>
{options.map((option, i) => (
<option key={i} value={option.value}>
{option.label}
</option>
))}
</select>
</form>
)
useForm
const handleChange = (e) => {
if (e.target.type === 'checkbox') {
setFormData((currentValues) => ({ ...currentValues, [e.target.name]: e.target.checked }));
} else {
setFormData((currentValues) => ({ ...currentValues, [e.target.name]: e.target.value }));
}
};