If you want to remove this scroll feature from all the number input fields of your project as whole or alternatively you could only import it into your component and this is the solution that worked for me.
First you create a custom hook like this one in your desired location in your project.
import { useEffect } from 'react';
const DisableNumInputScroll = () => {
const handleWheel = (event) => {
const { type } = event.target;
if(type === 'number'){
event.preventDefault();
}
}
useEffect(() => {
document.addEventListener('wheel', handleWheel, { passive: false });
return () => {
document.removeEventListener('wheel', handleWheel);
};
}, []);
return null;
};
export default DisableNumInputScroll;
Basically this adds a wheel event listener to the document and when the event occurs, it is checked if target of that event element is of type number. If yes, it will stop the default behavior which is responsible for the scroll increasing the number in input tag.
You can use this custom hook in your main App.js file like so,
import DisableNumInputScroll from './DisableNumInputScroll';
const App = () => {
return (
<>
<DisableNumInputScroll />
{/* Rest of your application */}
</>
);
};
export default App;