gpt4 book ai didi

javascript - 更改结束日期时如何触发方法

转载 作者:行者123 更新时间:2023-11-30 09:13:32 25 4
gpt4 key购买 nike

我正在使用 Airbnb react-dates .我已将它添加到我的项目中并且工作正常,组件如下所示:

<DateRangePicker
startDate={this.state.startDate} // momentPropTypes.momentObj or null,
startDateId="your_unique_start_date_id" // PropTypes.string.isRequired,
endDate={this.state.endDate} // momentPropTypes.momentObj or null,
endDateId="your_unique_end_date_id" // PropTypes.string.isRequired,
onDatesChange={this.onDatesChange} // PropTypes.func.isRequired,
focusedInput={this.state.focusedInput} // PropTypes.oneOf([START_DATE, END_DATE]) or null,
onFocusChange={focusedInput => this.setState({ focusedInput })} // PropTypes.func.isRequired,
daySize={50}
noBorder={true}
isOutsideRange={() => false}
/>

我刚刚将它添加到我的项目中,如您所见,我的方法 onDatesChange 一切正常,但我想知道如何在选择 END_DATE 时触发某些方法。

例如,我想在触摸结束日期时过滤一些内容...

最佳答案

您需要使用 onFocusChange onDatesChange <DateRangePicker> 的 Prop , 还有 componentDidUpdate() React 生命周期方法:

CodeSandbox

import React, { Component } from "react";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
import { DateRangePicker } from "react-dates";
import { START_DATE, END_DATE } from "react-dates/constants";

export default class Dates extends Component {
state = {
startDate: null,
endDate: null,
focusedInput: null
};

onDatesChange = ({ startDate, endDate }) =>
this.setState({ startDate, endDate });

onFocusChange = focusedInput => this.setState({ focusedInput });

componentDidUpdate(prevProps, prevState) {
if (
prevState.focusedInput !== this.state.focusedInput &&
this.state.focusedInput === END_DATE
) {
alert("End date is focused!"); // your code here
}

if (prevState.endDate !== this.state.endDate) {
alert("End date is changed!"); // your code here
}
}

render() {
const { startDate, endDate, focusedInput } = this.state;

return (
<DateRangePicker
startDate={startDate}
startDateId={START_DATE}
endDate={endDate}
endDateId={END_DATE}
onDatesChange={this.onDatesChange}
focusedInput={focusedInput}
onFocusChange={this.onFocusChange}
/>
);
}
}

关于javascript - 更改结束日期时如何触发方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56818981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com