[英]How to refer 'this' keyword in React Stateless Component
Am trying to create a React Stateless Components for input range day picker using library day-picker .我正在尝试使用库day-picker为输入范围日期选择器创建一个 React 无状态组件。
If I try to convert the Stateful Component to Stateless, the this
keyword is not accessible, since Stateless components doesn't have the scope for this
.如果我尝试将有状态组件转换为无状态,则无法访问this
关键字,因为无状态组件没有用于this
的 scope 。
Am very new to React and Hooks and gave my best to resolve, but somehow failed to resolve the problem, this is what I was trying.我对 React 和 Hooks 非常陌生,并尽我所能解决,但不知何故未能解决问题,这就是我正在尝试的。
Problem - Day picker range input is not working as expected.问题 - 日期选择器范围输入未按预期工作。 Always the calendar show start with current month.日历显示始终从当前月份开始。 But somehow it is starting from last year.但不知何故,它是从去年开始的。
import React, { useState } from 'react';
import DayPickerInput from 'react-day-picker/DayPickerInput';
import moment from 'moment';
import { formatDate, parseDate } from 'react-day-picker/moment';
import 'react-day-picker/lib/style.css';
const DayPickerRange = () => {
const [days, setDays] = useState({
from: new Date(),
to: new Date(today.getTime() + 24 * 60 * 60 * 1000)
});
function showFromMonth() {
const { from, to } = days;
if (!from) {
return;
}
if (moment(to).diff(moment(from), 'months') < 2) {
// this.to.getDayPicker().showMonth(from);
to.getDayPicker().showMonth(from);
}
}
const handleFromChange = from => {
// Change the from date and focus the "to" input field
setDays({ from, to }, showFromMonth);
};
const handleToChange = to => {
setDays({ from, to });
};
const { from, to } = days;
const modifiers = {
start: from,
end: to
};
return (
<div className="InputFromTo">
<DayPickerInput
value={from}
placeholder="From"
format="LL"
formatDate={formatDate}
parseDate={parseDate}
dayPickerProps={{
utc: true,
selectedDays: [from, { from, to }],
disabledDays: [{ before: new Date() }],
toMonth: to,
month: to,
modifiers,
numberOfMonths: 12,
onDayClick: () => to.getInput().focus()
}}
onDayChange={handleFromChange}
/>
<span className="InputFromTo-to">
<DayPickerInput
ref={el => {
days.to = el;
}}
value={to}
placeholder="To"
format="LL"
formatDate={formatDate}
parseDate={parseDate}
dayPickerProps={{
selectedDays: [from, { from, to }],
disabledDays: [{ before: new Date() }],
modifiers,
month: from,
fromMonth: from,
numberOfMonths: 12,
utc: true
}}
onDayChange={handleToChange}
/>
</span>
</div>
);
};
export default DayPickerRange;
There are multiple things, you need to figure out / take into consideration while converting class based component to functional component.在将基于 class 的组件转换为功能组件时,有很多事情需要弄清楚/考虑。
Don't initialize your state with new Date()
,不要使用new Date()
初始化您的 state ,
const [days, setDays] = useState({
from: new Date(),
to: new Date(today.getTime() + 24 * 60 * 60 * 1000)
});
Date format of new Date()
and date format of your DayPickerInput
is not same. new Date()
的日期格式和DayPickerInput
的日期格式不同。 So you need to keep it as undefined
/ convert the new Date()
to format your DayPickerInput
understands.因此,您需要将其保持为undefined
/转换new Date()
以格式化您的DayPickerInput
理解。
const [days, setDays] = useState({
from: undefined,
to: undefined
});
Another thing is, setState
in class based component and functional component work a bit differently.另一件事是,基于 class 的组件和功能组件中的setState
的工作方式略有不同。 setState
in functional component don't have callback.功能组件中的setState
没有回调。
This setState
is a bit wrong,这个setState
有点不对,
const handleFromChange = from => {
// Change the from date and focus the "to" input field
setDays({ from, to }, showFromMonth);
};
const handleToChange = to => {
setDays({ from, to });
};
Here showFromMonth
as callback will not work.这里showFromMonth
作为回调不起作用。 You need a separate useEffect
hook which will listen to state change and run side-effect / callback accordingly,您需要一个单独的useEffect
挂钩,它会监听 state 的变化并相应地运行副作用/回调,
const handleFromChange = from => {
// Change the from date and focus the "to" input field
//This is functional setState which will only update `from` value
setDays(days => ({
...days,
from
}));
};
const handleToChange = to => {
//This is functional setState which will only update `to` value
setDays(days => ({
...days,
to
}));
};
//This is useEffect hook which will run only when `to` value changes
useEffect(()=>{
showFromMonth();
},[days.to, showFromMonth])
You have provided ref
to your second date picker,您已将ref
提供给您的第二个日期选择器,
ref={el => {
days.to = el;
}}
You should create a ref
variable separately and not directly use state as ref
.您应该单独创建一个ref
变量,而不是直接使用 state 作为ref
。
let toInput = React.createRef();
ref={el => {
toInput = el;
}}
I have made some modifications to your code according to actual code you provided in question.我根据您提供的实际代码对您的代码进行了一些修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.