如果导入了events-polyfill,则React Datepicker无法正常工作

时间:2019-03-15 08:53:00

标签: javascript reactjs polyfills uncaught-typeerror react-datepicker

我已经完成了react-datepicker软件包的基本设置,并且可以正常工作。但是,在我们的应用程序中,我们也使用了events-polyfill包。同时使用这两个软件包时,react-datepicker无法正常工作。

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);

演示:https://codesandbox.io/s/pj3yl7o0wm

完整错误:

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)

这是events-polyfill index.js文件中突出显示的罪魁祸首:

options.once      = Boolean(options.once);

完全不知道是什么问题。

1 个答案:

答案 0 :(得分:1)

尝试更新events-polyfill程序包。

类似当前版本https://github.com/lifaon74/events-polyfill/blob/master/src/EventListenerInterceptor.js#L105的情况已解决此问题。

这里是带有修订https://github.com/lifaon74/events-polyfill/commit/a087bf90e2335cdad670d08499157c40ea73abdb的提交,目标是2.1.1,但是npm只有2.1.0

npm install from Git in a specific version