javascript - 如果导入了 events-polyfill,React Datepicker 将无法正常工作
问题描述
react-datepicker
我已经完成了软件包的基本设置并且它正在工作。但是,在我们的应用程序中,我们也在使用events-polyfill
package.json 。而当这两个包同时使用时,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);
完全不知道是什么问题。
解决方案
尝试更新 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
推荐阅读
- cmake - CMake 编译选项仅用于编译测试
- google-api - API 发现服务仅在 BigQuery 中返回错误
- java - 在不知道名字的情况下绑定参数查询
- javascript - 从html文件中的按钮从另一个文件中触发JS函数
- java - 正则表达式替换文本外部 html标记
- git - 如何在 gitlabci 管道期间知道更新的文件
- android - 安装的 Android PWA 和 Google Play 混合应用版本冲突
- css - Firefox 66 渲染问题:带有矢量效果的 SVG 圆圈:非缩放笔划;
- java - 如何流式传输和收集来自对象集合的实例方法?
- swift - 具有相同名称的 Swift 枚举案例