3

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
aditya81070
  • 678
  • 1
  • 5
  • 22
  • 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. – Carsten Massmann Jul 13 '21 at 05: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? – Drew Reese Jul 13 '21 at 05:49
  • No @DrewReese, I mean that in both React and Vanilla Javascript, when we enter `e` , the change event is not called. – aditya81070 Jul 13 '21 at 05: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? – Tushar Shahi Jul 13 '21 at 05:54
  • Yes @TusharShahi. – aditya81070 Jul 13 '21 at 05:55
  • `e` by itself isn't a valid number yet, but is part of a valid number, i.e. `1e2` is the value `100`, so `e` alone isn't enough to trigger an input change. React fundamentally handles inputs differently than HTML/JS. – Drew Reese Jul 13 '21 at 05:56
  • See [difference between onChange and onInput](https://stackoverflow.com/questions/38256332/in-react-whats-the-difference-between-onchange-and-oninput) – Drew Reese Jul 13 '21 at 06:02
  • Thanks, @DrewReese. Got it. If I want to prevent entering these characters the only way is to prevent typing these characters as I see in other SF answers. – aditya81070 Jul 13 '21 at 06:04

1 Answers1

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.

Tushar Shahi
  • 16,452
  • 1
  • 18
  • 39
  • 1
    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. – aditya81070 Jul 13 '21 at 06:09