liveBook · Manning

Number input

As explained in React Quickly, Second Edition

A number input is an HTML input control for entering a numeric value.

A number input is a controlled HTML form field for entering numeric values. In React, read evt.target.valueAsNumber instead of evt.target.value so the current value is handled as a number. section 9.2.5

Reading a numeric value

const [amount, setAmount] = useState(0); <input type="number" value={amount} onChange={(evt) => setAmount(evt.target.valueAsNumber)} /> section 9.2.5

Keep these distinctions in mind:

  • The regular value property returns a string, while valueAsNumber returns the numeric value. section 9.2.5
  • Number inputs include input types number and range; calendar inputs include date, datetime-local, month, time, and week. section 9.2.5
Figure 9.15 A table of various inputs available in HTML and React forms. Note how this includes mostly variations of input fields, but also a range of buttons and a drop-down.
09-15

Compare number inputs with other HTML and React form controls in the input reference table. section 9.2.5

How React Quickly, Second Edition covers it

How deep it goes
Explains value conversion and input categories, but stops short of a complete number-input component.
Examples
Uses add-form variants and contrasts number, range, date, datetime-local, month, time, and week inputs.
What it assumes
Assumes familiarity with React form events and controlled text inputs while teaching numeric value extraction.
What you can do after
Apply it via React controlled forms by reading numeric values through valueAsNumber.
How it connects
Connects number inputs with controlled inputs, text inputs, range inputs, and calendar inputs.

This explainer was generated by Manning's knowledge graph from the text of React Quickly, Second Edition. The section links point to the parts of the book it draws on.