Как задать значение свойству когда обьект ещё не инициализирован?
import React, { useState } from "react";
import "./App.css";
function App() {
const [state, setState] = useState({
// для заполнения Option
days: amountOf("days"),
months: amountOf("months"),
years: amountOf("years"),
// получение текущей даты
currentDay: new Date().getDate(),
currentMonth: new Date().getMonth(),
currentYear: new Date().getFullYear(),
// сегодня (день недели)
todayIs: getToday(),
})
const input1 = React.createRef();
const input2 = React.createRef();
const input3 = React.createRef();
function amountOf(prop) {
switch (prop) {
case "days":
let startDays = 1;
const days = [];
while (startDays <= 30) {
days.push(startDays++);
}
return days;
case "months":
let startMonth = 1;
const months = [];
while (startMonth <= 12) {
months.push(startMonth++);
}
return months;
case "years":
let startYears = 2016;
const years = [];
while (startYears <= 2022) {
years.push(startYears++);
}
return years;
}
}
function getToday() {
let date = new Date(
state.currentYear,
state.currentMonth,
state.currentDay
);
date = date.getDay();
switch (date) {
case 0:
return "Sunday";
case 1:
return "Monday";
case 2:
return "Tuesday";
case 3:
return "Wednesday";
case 4:
return "Thursday";
case 5:
return "Friday";
case 6:
return "Saturday";
}
}
function handleClick() {}
return (
<>
<select
ref={input1}
onChange={handleClick}
defaultValue={state.currentDay}
>
{state.days.map((item, i) => {
return <option key={i}>{item}</option>;
})}
</select>
<select
ref={input2}
onChange={handleClick}
defaultValue={state.currentMonth}
>
{state.months.map((item, i) => {
return <option key={i}>{item}</option>;
})}
</select>
<select
ref={input3}
onChange={handleClick}
defaultValue={state.currentYear}
>
{state.years.map((item, i) => {
return <option key={i}>{item}</option>;
})}
</select>
<p>{}</p>
</>
);
}
export default App;
Задача кода: вывести день недели указаный в select-ах и по умолчанию при заходе на страницу выводить день недели сегодня. Так как мне на основании полученых данных в state присвоить todayIs если обьект на момент вызова getToday() обьект ещё не готов. Возможно я неправильно подошёл к вопросу.)