[英]React Day Picker months and weekdays_long
I am using "react": "^18.2.0", "react-day-picker": "^8.1.0",
and I am trying to change the months and days name in French, but when I pass my arrays to my component <DayPicker/>
, nothing changes.我正在使用
"react": "^18.2.0", "react-day-picker": "^8.1.0",
我正在尝试更改法语的月份和日期名称,但是当我将 arrays 传递给我的组件<DayPicker/>
,没有任何变化。 When I log my arrays, I can see it good.当我登录我的 arrays 时,我可以看到它很好。
All the answers I found for this problem or for old versions of react-day-picker.我为这个问题或旧版本的 react-day-picker 找到的所有答案。 Can somebody help me please?
有人可以帮我吗?
From another component of my site, I import <Message/>
, inside which I import my function <DayPicking/>
declared above, to create the <DayPicker/>
从我网站的另一个组件中,我导入
<Message/>
,在其中我导入上面声明的 function <DayPicking/>
,以创建<DayPicker/>
I don't have any error or warnings, but the months and days stay the default ones我没有任何错误或警告,但月份和日期保持默认值
Sorry for my English, I hope it's clear, and thank you very much for your help对不起我的英语,我希望它很清楚,非常感谢你的帮助
import React, {useRef} from "react";
import PropTypes from "prop-types";
import {addMonths} from "date-fns";
import {DayPicker} from 'react-day-picker'
const DayPicking = ({
MONTHS,
WEEKDAYS_LONG,
WEEKDAYS_SHORT,
selectedDay,
setSelectedDay,
ref
}) => {
const today = new Date();
const nextMonth = addMonths(new Date(), 1)
console.log(MONTHS) // gives back the array with months
console.log(WEEKDAYS_LONG) // gives back the weekdays array also
return (
<div ref={ref}>
<DayPicker
mode={"single"}
selected={selectedDay}
onSelect={setSelectedDay}
fromDate={today}
months={MONTHS}
weekdaysLong={WEEKDAYS_LONG}
weekdaysShort={WEEKDAYS_SHORT}
onDayClick={handleDayClick}
selectedDays={selectedDay}
disabledDays={[
{before: new Date(new Date().getTime() + (24 * 60 * 60 * 1000))},
{daysOfWeek: [0]}
]}
firstDayOfWeek={1}
/>
</div>
);
};
const Messages = ({
selectedDay,
setSelectedDay
}) => {
const MONTHS = [
"Janvier",
"Février",
"Mars",
"Avril",
"Mai",
"Juin",
"Juillet",
"Août",
"Septembre",
"Octobre",
"Novembre",
"Décembre",
];
const WEEKDAYS_LONG = [
"Dimanche",
"Lundi",
"Mardi",
"Mercredi",
"Jeudi",
"Vendredi",
"Samedi",
];
const WEEKDAYS_SHORT = ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"];
const lastListElement = useRef(null);
console.log(MONTHS)
return (
<>
<DayPicking
ref={lastListElement}
MONTHS={MONTHS}
WEEKDAYS_LONG={WEEKDAYS_LONG}
WEEKDAYS_SHORT={WEEKDAYS_SHORT}
handleDayClick={handleDayClick}
selectedDay={selectedDay}
setSelectedDay={setSelectedDay}
/>
</>
);
};
Messages.propTypes = {
selectedDay: PropTypes.oneOfType([PropTypes.string, PropTypes.instanceOf(Date)]),
setSelectedDay: PropTypes.func
};
export default Messages;
Had a similar issue and was told to use the formatter prop: https://react-day-picker.js.org/api/types/Formatters有类似的问题,并被告知使用 formatter 道具: https://react-day-picker.js.org/api/types/Formatters
Create a function called formatWeekdayName and import the DateFormatter type from react-day-picker.创建一个名为 formatWeekdayName 的 function 并从 react-day-picker 导入 DateFormatter 类型。 You'll also need to import the format function from date-fns.
您还需要从 date-fns 导入格式 function。
Then, you can do the following:然后,您可以执行以下操作:
import { format } from 'date-fns';
import frenchLocale from 'date-fns/locale/fr';
import { DateFormatter} from 'react-day-picker';
export const Component = () => {
...
const formatWeekdayName: DateFormatter = (day) => {
return format(day, 'EEE', { locale: frenchLocale });
};
return (
<DayPicker formatters={{ formatWeekdayName }};
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.