I have class component now I want to convert it to function component so I started with refactoring constructor
here is my class component
class EventCalendar extends React.Component {
constructor(props) {
super(props);
this.state = {
today: this.getToday(),
};
}
getToday() {
var today = new Date();
return {
day: today.getDate(),
month: today.getMonth(),
year: today.getFullYear(),
};
}
renderCalendarDays() {
return this.getDaysWithEvents().map((day, index) => {
const isToday = Calendar.interval(day, this.state.today) === 1;
return (
<CalendarDay
day={day}
isToday={isToday}
/>
);
});
}
render() {
return (
{this.renderCalendarDays()}
</div>
);
}
}
Here is my refactored functional component from class
const EventCalendar =props=> {
const [today, setToday] = useState();
const calendar = new Calendar({siblingMonths: true, });
useEffect(() => {
setToday();
},[])
const setToday =()=>{
var today = new Date();
return {
day: today.getDate(),
month: today.getMonth(),
year: today.getFullYear(),
};
}
const renderCalendarDays = ()=>{
return getDaysWithEvents().map((day, index) => {
const isToday = Calendar.interval(day, today) === 1;
return (
<CalendarDay
day={day}
isToday={isToday}
/>
);
});
}
return (
<div className="flexContainer">
{renderCalendarDays()}
</div>
);
}
Unfortunately, I am struggling to make this work, can someone help me, what am I doing wrong here?