4

I found a strange behaviour when working with input fields with type="number" in React. I have a really simple React component with one controlled number input field and as e.target.value is always a string, I am parsing it to make it a number.

function App() {
  const [value, setValue] = React.useState("");

  const handleInputChange = (e) => {
    const input = e.target.value;
    console.log(input);
    const parsedValue = parseInt(input, 10);
    console.log(parsedValue);
    setValue(isNaN(parsedValue) ? input : parsedValue);
  };
  return (
      <form>
        <input
          type="number"
          min={1}
          max={100}
          value={value}
          pattern="\d+"
          onChange={handleInputChange}
        />
      </form>
  );
}

The handleInputChange function is called every time I enter a new value input, but it is not called when I type characters e, '+', or .. For the +, ., and -, I understand that we expect some digit after that so calling onChange doesn't make much sense but for the e, it should have called onChange.

I tried to simulate the same behaviour with vanilla javascript but there change event is only called if I increase/decrease the number using arrow keys. Here is the [codepen example for the same][1]

Can someone explain this behaviour and how can I detect typing e in number input?

Edit: I think there is some confusion with this question. I want to understand two things about change event behaviour for number input fields.

  1. Why React doesn't call onChange when I type e, '+', '-', and '.' ?
  2. Why change event is not called when I type some value in input in Vanilla Javascript? [1]: https://codepen.io/aditya81070/pen/WNjoLao
8
  • I am not familiar with reactjs, but generally the input event is more practical here (at least in Vanilla-JavaScript), as it is fired immediately anytime anything changes in the input element. Commented Jul 13, 2021 at 5:46
  • Your question is unclear. Are you asking why number inputs work one way in React/JSX? Or why they work differently in vanilla HTML/JS? What are you really asking for? Commented Jul 13, 2021 at 5:49
  • No @DrewReese, I mean that in both React and Vanilla Javascript, when we enter e , the change event is not called. Commented Jul 13, 2021 at 5:54
  • The OP has 2 doubts one why is onChange not triggered with e in react and 2nd why is onChange not having correct e.target.value in native JS with e. Correct? Commented Jul 13, 2021 at 5:54
  • Yes @TusharShahi. Commented Jul 13, 2021 at 5:55

1 Answer 1

2

Any text ending with e is not a number. It is like 1e3 or 2e4. So if your number is ending with 1e, it is not valid. Javascript is preventing you from doing so that is why both in your native code and in react, although the onChange(/onchange) is triggered the e.target.value is "". So you can not make use of it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah this was answered by @DrewReese in the comments. The same thing happens with +, -, . . Also, I had another question about the onChange difference in React vs Vanilla Javascript and @DrewReese shared an answer for that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.