A
A
Artyom Novolodsky2017-10-14 06:23:56
React
Artyom Novolodsky, 2017-10-14 06:23:56

React. How to store number type in option value?

...
<select name="field" value={1} onChange={this.changeForm}>
<option key={0} value={1}>1</option>
<option key={1} value={2}>2</option>
</select>
...

in changeForm , the event.target.value field is of type string , but it needs to be number .
How to solve trouble?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Negwereth, 2017-10-14
@Tmch

No way. In attributes, all values ​​are stored as strings.
Therefore, I would advise keeping different handlers for different data types in the input. Well or there on isNaN of check to do.

...
const number = isNaN(+value) ? value : +value;
...

A
alvvi, 2017-10-14
@alvvi

What's stopping you from just doing it ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question