Search code examples
javascriptreactjsinternet-explorer-9internet-explorer-10

React onInput - IE9/10 event.target.value is empty?


A simple controlled input is not updating value based on user input when "onInput" is used as the listening event (in IE9/10). "onChange" works properly, but "onInput" has been selected as our event in order to solve for pasting values in IE11.

class TestComp extends React.Component {
  constructor(props){
    super(props);
    this.state = { value: "" };
    this.updateValue = this.updateValue.bind(this);
  }

  render(){
    return <input onInput={this.updateValue} value={this.state.value} />;
  }

  updateValue(evt){
    this.setState({ value: evt.target.value });
  }
};

https://codepen.io/anon/pen/KmJPGR

Is this an improper implementation of a controlled input? If yes, how can this be fixed? If no, what alternatives exist?


Solution

  • I would still use onChange although I cannot test it right now I've heard that onInput has some issues with IE9. According to React docs:

    For <input> and <textarea>, onChange supersedes — and should generally be used instead of the DOM's built-in oninput event handler.

    What about creating a fix for the onChange in IE11 instead of using other listener? A hack, although I don't recommend doing this, could probably be using onChange and onInput at the same time. Remember that Chrome will trigger both calls so I would recommend passing onInput only when userAgent matches.

    I've also found this polyfill that fixes some of these issues (with deleting, clearing, cut the value) Couldn't test it, though: https://github.com/buzinas/ie9-oninput-polyfill.