I used dayjs lib, I pass two props to component, but inside component I got wrong date, server gave me two dates:
horizontStart: '2025-01-29T00:00:00Z'
horizontEnd: '2025-04-01T00:00:00Z'
inside component GanttChartOrders I use useState to write this constant via dayjs, like
const [initialStart, setInitialStart] = useState(dayjs(horizontStart))
const [initialEnd, setInitialEnd] = useState(dayjs(horizontEnd))
but initialEnd
is initiated as today's date, and I can't figure out why, because I have const enddate
.
It seems like I always have default values and it not updating when I get data. But in console.log I see updated values.
<GanttChartOrders
horizontStart={new Date(gantt?.horizont_start || new Date(new Date().getFullYear(), 0, 1))}
horizontEnd={new Date(gantt?.horizont_end || new Date())}
data={gantt || []}
isTooltip={true}
tooltip={tooltip}
inferringId={inferringId}
filterType={filterType}
ganttType={ganttType}
/>
//logs inside GanttChartOrders
// initial start and end dates
const [initialStart, setInitialStart] = useState(dayjs(horizontStart))
const [initialEnd, setInitialEnd] = useState(dayjs(horizontEnd))
console.log(horizontStart)
console.log(horizontEnd)
console.log(initialStart)
console.log(initialEnd)
//logs
Wed Jan 29 2025 03:00:00 GMT+0300 (Moscow Standard Time)
Tue Apr 01 2025 03:00:00 GMT+0300 (Moscow Standard Time)
M2 {$L: 'en', $u: undefined, $d: Wed Jan 01 2025 00:00:00 GMT+0300 (Moscow Standard Time), $y: 2025, $M: 0, …}
M2 {$L: 'en', $u: undefined, $d: Tue Mar 04 2025 18:18:41 GMT+0300 (Moscow Standard Time), $y: 2025, $M: 2, …}
I used dayjs lib, I pass two props to component, but inside component I got wrong date, server gave me two dates:
horizontStart: '2025-01-29T00:00:00Z'
horizontEnd: '2025-04-01T00:00:00Z'
inside component GanttChartOrders I use useState to write this constant via dayjs, like
const [initialStart, setInitialStart] = useState(dayjs(horizontStart))
const [initialEnd, setInitialEnd] = useState(dayjs(horizontEnd))
but initialEnd
is initiated as today's date, and I can't figure out why, because I have const enddate
.
It seems like I always have default values and it not updating when I get data. But in console.log I see updated values.
<GanttChartOrders
horizontStart={new Date(gantt?.horizont_start || new Date(new Date().getFullYear(), 0, 1))}
horizontEnd={new Date(gantt?.horizont_end || new Date())}
data={gantt || []}
isTooltip={true}
tooltip={tooltip}
inferringId={inferringId}
filterType={filterType}
ganttType={ganttType}
/>
//logs inside GanttChartOrders
// initial start and end dates
const [initialStart, setInitialStart] = useState(dayjs(horizontStart))
const [initialEnd, setInitialEnd] = useState(dayjs(horizontEnd))
console.log(horizontStart)
console.log(horizontEnd)
console.log(initialStart)
console.log(initialEnd)
//logs
Wed Jan 29 2025 03:00:00 GMT+0300 (Moscow Standard Time)
Tue Apr 01 2025 03:00:00 GMT+0300 (Moscow Standard Time)
M2 {$L: 'en', $u: undefined, $d: Wed Jan 01 2025 00:00:00 GMT+0300 (Moscow Standard Time), $y: 2025, $M: 0, …}
M2 {$L: 'en', $u: undefined, $d: Tue Mar 04 2025 18:18:41 GMT+0300 (Moscow Standard Time), $y: 2025, $M: 2, …}
Share
Improve this question
edited Mar 4 at 15:40
David Abramov
asked Mar 4 at 15:09
David AbramovDavid Abramov
537 bronze badges
3
|
1 Answer
Reset to default 0The problem was that I needed to use UseEffect to get the updated values, but it's not that obvious in my case.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745036450a4607546.html
gnatt?.horizonEnd
isundefined
, asDate()
's default construction generates aDate
object for todays date. Can you add a contextualconsole.log
of yourgnatt
object? – Sa'id Kharboutli Commented Mar 4 at 15:15horizon_start
its the first of January because your fallback is aDate
object set to the first of January of the current year (using anotherDate
object to get that data, which you likely do not need to do). Thehorizon_end
is the other fallback value, the currentDatetime
. This again means that yourgantt
object is likely undefined. I don't see any mention of thegantt
object besides when you're reading from it. Where are you writing the values to it? Can you loggantt
? – Sa'id Kharboutli Commented Mar 4 at 16:01