Search code examples
javascriptreactjspolyfillstypeerrorreact-datepicker

React Datepicker doesn't work properly if events-polyfill is imported


I have done the basic set-up for the react-datepicker package and it was working. However, in our app, we are also using events-polyfill package. And when these two packages are used at the same time, react-datepicker doesn't work properly.

import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "events-polyfill";

import "react-datepicker/dist/react-datepicker.css";
import "./styles.css";

function App() {
  return <Example />;
}

// CSS Modules, react-datepicker-cssmodules.css
// import 'react-datepicker/dist/react-datepicker-cssmodules.css';

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      startDate: new Date()
    };
    this.handleChange = this.handleChange.bind(this);
  }

  handleChange(date) {
    this.setState({
      startDate: date
    });
  }

  render() {
    return (
      <DatePicker
        selected={this.state.startDate}
        onChange={this.handleChange}
      />
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Demo: https://codesandbox.io/s/pj3yl7o0wm

Full error:

Uncaught TypeError: Cannot read property 'once' of null
    at Object.module.exports.EventListenerInterceptor.normalizeListenerOptions (index.js:111)
    at Object.module.exports.EventListenerInterceptor.normalizeListenerArguments (index.js:122)
    at HTMLDocument.target.addEventListener (index.js:161)
    at react-onclickoutside.es.js:226
    at Array.forEach (<anonymous>)
    at onClickOutside._this.enableOnClickOutside (react-onclickoutside.es.js:225)
    at onClickOutside.componentDidMount (react-onclickoutside.es.js:296)
    at commitLifeCycles (react-dom.development.js:14362)
    at commitAllLifeCycles (react-dom.development.js:15463)
    at HTMLUnknownElement.callCallback (react-dom.development.js:100)

And this is highlighted in the events-polyfill index.js file as the culprit:

options.once      = Boolean(options.once);

Totally clueless what's the issue.


Solution

  • Try updating events-polyfill package.

    Looks like current version https://github.com/lifaon74/events-polyfill/blob/master/src/EventListenerInterceptor.js#L105 has a fix for this case.

    Here is the commit with the fix https://github.com/lifaon74/events-polyfill/commit/a087bf90e2335cdad670d08499157c40ea73abdb It is aimed for 2.1.1 but npm has only 2.1.0

    npm install from Git in a specific version