🔨Building your calendar
react-datepicker main goal is to let you control how you want your date picker to look & fill.
This means you need to render the element like the days grid, years selector and next/previous month buttons.
Lets start with creating a new component file and calling the useCalendar hook, currently only getting it's displayedMonths and register variables. We will also render our initial days grid.
import useCalendar from "../useCalendar";
import "./style.css";
const MyFirstCalendar = () => {
const { displayedMonths, register } = useCalendar();
return (
<div>
<h1>My first calendar</h1>
{displayedMonths.map((month) => {
return (
<div key={month.name}>
<h2>
{month.name} {month.year}
</h2>
<div className="daysGrid">
{month.days.map((day) => {
return <div {...register(month, day)}>{day.number}</div>;
})}
</div>
</div>
);
})}
</div>
);
};
export default MyFirstCalendar;.daysGrid {
display: grid;
grid-template-columns: repeat(7, 1fr);
gap: 16px;
width: fit-content;
margin: 0 auto;
}What's going on here?
displayedMonthswill always hold the months that should be visible to the user.registeris a function that returns all the props you need for the connection of your UI - the day component with theuseCalendarhook. It exposes stuff likeonChange, ARIA attributes and more.Each
Monthholds few parameters, but for the beginning we are using the days array which includesDayobject, and we render it on the screen.The wrapper
divis a grid, which is a standard way of calendar UI structure, but you can definitely try to render what you want!
You should now see your very first calendar! 🔥

Last updated