【问题标题】:React Datepicker doesn't work properly if events-polyfill is imported如果导入了 events-polyfill,React Datepicker 将无法正常工作
【发布时间】:2019-08-06 07:21:39
【问题描述】:

我已经完成了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);

完全不知道是什么问题。

【问题讨论】:

    标签: javascript reactjs polyfills typeerror react-datepicker


    【解决方案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

    【讨论】:

    • 是的,我看到了,如果 npm 的最新版本是 2.1.0,我如何更新到最新版本?
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    相关资源
    最近更新 更多