Simpler approach is to have a fallback value be an empty string. Works for type=number as well.

<input type="number" value={estimatedHours || ''} />
Answer from GetSwifty on Stack Overflow
🌐
GitHub
github.com › facebook › react › issues › 7779
Proper Pattern for Controlled Input[type="number"] with blank initialization · Issue #7779 · facebook/react
September 20, 2016 - Warning: NumInput is changing an uncontrolled input of type number to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. Reactions are currently unavailable ·
Author: facebook
Discussions

reactjs - How to make an input of type number a controlled component in react - Stack Overflow
0 How to implement an input type number with controllers (with redux form) 5 React: Bind number input to state (handle intermediate values) More on stackoverflow.com
🌐 stackoverflow.com
Only numbers. Input number in React - javascript
I should also mention, changing the input type from 'number' to 'tel' accomplishes many of the objectives sought by using a number input. You still get a numeric keypad on mobile, and you can still control the input fully, regardless of what input you provide it. More on stackoverflow.com
🌐 stackoverflow.com
reactjs - React controlled <type='number'> input allows typing in letter? - Stack Overflow
I am writing an app that contains a controlled input that has the type 'number'. I can still type letters in the input field, but the e.target.value just gets emptied. Does anyone know how I can di... More on stackoverflow.com
🌐 stackoverflow.com
Only numbers. Input number in React
I should also mention, changing the input type from 'number' to 'tel' accomplishes many of the objectives sought by using a number input. You still get a numeric keypad on mobile, and you can still control the input fully, regardless of what input you provide it. More on stackoverflow.com
🌐 stackoverflow.com
December 4, 2020
🌐
React
react.dev › reference › react-dom › components › input
<input> – React
step: A positive number or an 'any' string. Specifies the distance between valid values. ... Checkboxes need checked (or defaultChecked), not value (or defaultValue). If a text input receives a string value prop, it will be treated as controlled.
🌐
Vlad-ignatov
vlad-ignatov.github.io › react-numeric-input
React number input examples
<NumericInput className="form-control" style={ false }/> Typical Usage · Most of the time you will need to specify min, max and value: <NumericInput min={0} max={100} value={50}/> Working with floats · You can use to use step and precision props to make your input working with floating point numbers: <NumericInput step={0.1} precision={2} value={50.3}/> Custom format ·
🌐
Wecodeyoursite
blog.wecodeyoursite.com › home › how to create a react number input control
How to create a react number input control - Web Development
June 26, 2021 - You can make sure react number input control only accept numbers by handling keypress event handler of the input. Event handlers always gives the key code when users type something.
🌐
GitHub
github.com › d-asensio › use-controlled-input-number
GitHub - d-asensio/use-controlled-input-number: React hook to turn numeric input behavior into pretty much what you expect
function ControlledInput () { const [value, setValue] = useState(1) const inputProps = useControlledInputNumber({ value, // The value will always be of type number onChange: setValue }) return ( <input type='number' {...inputProps} /> ) }
Author: d-asensio
Find elsewhere
🌐
npm
npmjs.com › package › @react-input › number-format
react-input/number-format
The input-number-format event and onNumberFormat method are no longer available in newer versions, focusing work on only using React's own events and methods such as onChange, since the input-number-format event and onNumberFormat method cannot be explicitly coordinated with React's events and methods, making such usage and event firing order non-obvious.
      » npm install @react-input/number-format
    
Published: Dec 05, 2024
Version: 2.0.3
🌐
GitHub
github.com › react-component › input-number
GitHub - react-component/input-number: 🔢 Accessible React number input with precision, formatting, keyboard, and wheel support.
🔢 Accessible React number input with precision, formatting, keyboard, and wheel support. ... Controlled and uncontrolled numeric input modes.
Author: react-component
Top answer
1 of 2
2

You can use this code:

<input id = 'prepFormInput' type='number' className='prepNumber' value={props.prepField.numeral} onChange={props.handlePrep} placeholder='prep time' onkeydown="javascript: return ['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code) ? true : !isNaN(Number(event.key)) && event.code!=='Space'" />

This will avoids all characters that are not numbers!

This part make's input accept only numbers:

!isNaN(Number(event.key))

This part make's accept the arrow and delete keys:

['Backspace','Delete','ArrowLeft','ArrowRight'].includes(event.code)

This part make's Firefox disallow spaces in numbers:

event.code!=='Space'
2 of 2
0

(For react Applications)

If someone has text input and wants to accept only whole numbers. You can use the following code.

const numberInputKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
    const eventCode = event.code.toLowerCase();
    if (!(event.code !== null
    && (eventCode.includes("digit")
        || eventCode.includes("arrow")
        || eventCode.includes("home")
        || eventCode.includes("end")
        || eventCode.includes("backspace") 
        || (eventCode.includes("numpad") && eventCode.length === 7)))
    ) {
    event.preventDefault();
    }
};

I have compared the length of eventCode to 7 when it includes numpad because its actual value is like numpad0, numpad1 ... numpad9

This function can be specified in onKeyDown such as

<input
    id="myNumberInput"
    type="text"
    placeholder="numeric input"
    onKeyDown={numberInputKeyDown}
/>

To extent field to accept decimals or negative numbers you can console.log the eventCode in numberInputKeyDown to check your required key code and add it in if statement.

🌐
npm
npmjs.com › package › react-numeric-input
react-numeric-input - npm
It provides access to the value as number to be used by scripts. In this react component this becomes even more desirable as the display value might be formatted and have nothing in common with the underlying value meaning that one might need ...
      » npm install react-numeric-input
    
Published: Jan 13, 2018
Version: 2.2.3
🌐
npm
npmjs.com › package › rc-input-number
rc-input-number - npm
React input-number component. Latest version: 9.5.0, last published: a year ago. Start using rc-input-number in your project by running `npm i rc-input-number`. There are 614 other projects in the npm registry using rc-input-number.
      » npm install rc-input-number
    
Published: Apr 11, 2025
Version: 9.5.0
🌐
Bobby Hadz
bobbyhadz.com › blog › react-only-number-input
Create a Numbers only Input field in React.js | bobbyhadz
We set the onChange prop on the input field, so every time its value changes, the handleChange function is invoked. ... In our handleChange() function, we update the num state variable and check if the value is a valid number.
🌐
CodeSandbox
codesandbox.io › s › react-controlled-number-input-7o0bi
react-controlled-number-input - CodeSandbox
January 30, 2020 - react-controlled-number-input by melaniam using react, react-dom, react-scripts
Published: Oct 31, 2019
Author: melaniam
🌐
DEV Community
dev.to › morewings › lets-create-a-better-number-input-with-react-1j0m
Let's create a better Number Input with React and Tailwind CSS - DEV Community
March 5, 2025 - Complexity. Valid numbers are not just digits. Number input allows negative (-100) and floating point (0.01) values, as well as scientific notation (-2.3e4).
🌐
GitHub
github.com › facebook › react › issues › 29862
[React 19] Controlled number input does not update defaultValue when value prop has changed · Issue #29862 · facebook/react
June 11, 2024 - Summary Controlled inputs with the type=number are not setting the defaultValue when the value state is altered. This causes the state and value to become out of sync when a form is reset (for instance after a form action is sent). https...
Author: facebook
🌐
Reddit
reddit.com › r/nextjs › how do i work with input type of number in react hook form?
r/nextjs on Reddit: How do I work with input type of number in react hook form?
February 27, 2024 -

Hi, I am using shadcn with react hook form. My form has two input fields. One is string and other is number. Here's my code.
If I am not using value and onChange inside Input then it gives me this error.
A component is changing an uncontrolled input to be controlled

If I am setting value as empty string then I am not able to clear the input after entering the value. Value is 0 when I try to clear the field.

Any way to resolve this??

🌐
Material Tailwind
material-tailwind.com › docs › react › input-number
Tailwind CSS Input Number for React - Material Tailwind
... Adjust the number using the + and - controls. import React from "react"; import { Input, IconButton, Typography } from "@material-tailwind/react"; export function InputAmountButtons() { const [value, setValue] = React.useState(0); return ( <div className="w-80"> <Typography variant="small" ...