我想显示一个从反应日期-选择的具体日期范围。我已经尝试了他们的文档站点演示代码。但是这段代码不能工作,.what会是解决方案吗?下面是我的示例代码
import React, { useState } from 'react';
import { addDays, format } from 'date-fns';
import { DateRange, DayPicker } from 'react-day-picker';
const pastMonth = new Date();
const App = () => {
const defaultSelected: DateRange = {
from: pastMonth,
to: addDays(pastMonth, 4)
};
const [range, setRange] = useState < DateRange | undefined > (defaultSelected);
let footer = <p>Please pick the first day.</p>;
if (range?.from) {
if (!range.to) {
footer = <p>{format(range.from, 'PPP')}</p>;
} else if (range.to) {
footer = (
<p>
{format(range.from, 'PPP')}–{format(range.to, 'PPP')}
</p>
);
}
}
return (
<div>
<DayPicker
mode="range"
defaultMonth={pastMonth}
selected={range}
footer={footer}
onSelect={setRange}
/>
</div>
);
};
export default App;
`
我已经尝试过对日期选择器文档站点作出反应,选择一个日期演示代码的范围,我想显示和选择基于react: 18.2.0
的日期范围。
发布于 2022-11-16 08:00:01
我从prev代码中删除所有类型,作为js代码使用。试试这个:
import React, { useState } from 'react';
import { addDays, format } from 'date-fns';
import { DayPicker } from 'react-day-picker';
const pastMonth = new Date();
const App = () => {
const defaultSelected = {
from: pastMonth,
to: addDays(pastMonth, 4)
};
const [range, setRange] = useState(defaultSelected);
let footer = <p>Please pick the first day.</p>;
if (range?.from) {
if (!range.to) {
footer = <p>{format(range.from, 'PPP')}</p>;
} else if (range.to) {
footer = (
<p>
{format(range.from, 'PPP')}–{format(range.to, 'PPP')}
</p>
);
}
}
return (
<div>
<DayPicker
mode="range"
defaultMonth={pastMonth}
selected={range}
footer={footer}
onSelect={setRange}
/>
</div>
);
};
export default App;
https://stackoverflow.com/questions/74458252
复制相似问题