2 回答
TA贡献1887条经验 获得超5个赞
您只是缺少状态更改。你可以像下面这样
import React from "react";
import "./styles.css";
import { render } from "react-dom";
import { DateRangePicker } from "react-date-range";
import "react-date-range/dist/styles.css"; // main style file
import "react-date-range/dist/theme/default.css"; // theme css file
export default class App extends React.Component {
constructor(props) {
super(props)
this.state = {
selectionRange : {
startDate: new Date(),
endDate: new Date(),
key: "selection"
}
}
}
handleSelect=(ranges)=> {
this.setState({
selectionRange : {
startDate: ranges.selection.startDate,
endDate: ranges.selection.endDate,
key: "selection"
}
})
console.log(ranges.selection.endDate);
}
render() {
return (
<DateRangePicker ranges={[this.state.selectionRange]} onChange={this.handleSelect} />
);
}
}
render(<App />, document.querySelector("#root"));
TA贡献1797条经验 获得超4个赞
缺少更新和绑定到的状态管理DateRangePicker:
import React from "react";
import "./styles.css";
import { render } from "react-dom";
import { DateRangePicker } from "react-date-range";
import "react-date-range/dist/styles.css"; // main style file
import "react-date-range/dist/theme/default.css"; // theme css file
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
range: {
startDate: new Date(),
endDate: new Date(),
key: "selection"
}};
this.handleSelect = this.handleSelect.bind(this);
}
handleSelect(ranges) {
this.setState({
range: ranges.selection
});
}
render() {
return (
<DateRangePicker ranges={[this.state.range]} onChange={this.handleSelect} />
);
}
}
render(<App />, document.querySelector("#root"));
添加回答
举报