我正在使用 react-day-picker 库在我的网站上显示日期选择器。我想听听日期何时更改。我知道您可以将 onDayChange 钩子添加到DayPickerInput
组件中,但我想监听组件之外的更改。
示例:(https://codesandbox.io/s/react-day-picker-base-mpcqy)
import React from "react";
import ReactDOM from "react-dom";
import DayPickerInput from "react-day-picker/DayPickerInput";
import "react-day-picker/lib/style.css";
function Example() {
return (
<div>
<h3>DayPickerInput</h3>
<DayPickerInput placeholder="DD/MM/YYYY" format="DD/MM/YYYY" />
</div>
);
}
ReactDOM.render(<Example />, document.getElementById("root"));
var input = document.getElementsByTagName("input")[0];
input.addEventListener("change", () => {
console.log("changed");
});
我希望 on change 处理程序被解雇。我不确定为什么不是。如何聆听价值变化?