Default Values
In the previous part, you declared the default state value as some sort of null
. What if you want to use a day(range) as your default state value? Well, it's pretty straightforward. you need to pass your intended day in the correct format instead of null
.
Single Date Default Value
import React, { useState } from "react";
import "react-modern-calendar-datepicker/lib/DatePicker.css";
import { Calendar } from "react-modern-calendar-datepicker";
const App = () => {
const defaultValue = {
year: 2019,
month: 10,
day: 5,
};
const [selectedDay, setSelectedDay] = useState(defaultValue);
return (
<Calendar
value={selectedDay}
onChange={setSelectedDay}
shouldHighlightWeekends
/>
);
};
export default App;
SMTWTFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Range Date Default Value
import React, { useState } from "react";
import "react-modern-calendar-datepicker/lib/DatePicker.css";
import { Calendar } from "react-modern-calendar-datepicker";
const App = () => {
const defaultFrom = {
year: 2019,
month: 3,
day: 4,
};
const defaultTo = {
year: 2019,
month: 3,
day: 7,
};
const defaultRange = {
from: defaultFrom,
to: defaultTo,
};
const [selectedDayRange, setSelectedDayRange] = useState(
defaultRange
);
return (
<Calendar
value={selectedDayRange}
onChange={setSelectedDayRange}
shouldHighlightWeekends
/>
);
};
export default App;
SMTWTFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Multiple Date Default Value
import React, { useState } from "react";
import "react-modern-calendar-datepicker/lib/DatePicker.css";
import { Calendar } from "react-modern-calendar-datepicker";
const App = () => {
const preselectedDays = [
{
year: 2019,
month: 10,
day: 2,
},
{
year: 2019,
month: 10,
day: 15,
},
{
year: 2019,
month: 10,
day: 30,
},
]
const [selectedDayRange, setSelectedDayRange] = useState(
preselectedDays
);
return (
<Calendar
value={selectedDayRange}
onChange={setSelectedDayRange}
shouldHighlightWeekends
/>
);
};
export default App;
SMTWTFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31