I am not understanding how Luxon works…
I am using Redux Toolkit. I initialize the state like this
import {DateTime} from "luxon"
interface TestStateProps {
startDate: DateTime,
endDate: DateTime,
}
const initialTestState: TestStateProps {
startDate: DateTime.now(),
endDate: DateTime.now(),
}
export const testSlice = createSlice({
name: 'test',
initialTestState,
reducers: {
addDay: (state, action: PayloadAction<number>) => {
console.log(state.startDate instanceof DateTime)
console.log(JSON.stringify(state.startDate))
console.log(state.startDate.plus({days: action.payload }))
}
}
})
And now, the result of the console.log
is quite unexpected
console.log(state.startDate instanceof DateTime)
false
Why is it false? I initialized it with DateTime.now()
console.log(JSON.stringify(state.startDate))
{“ts”:1724911978667,”_zone”:{},”loc”:{“locale”:”en-DE”,”numberingSystem”:null,”outputCalendar”:null,”weekSettings”:null,”intl”:”en-DE”,”weekdaysCache”:{“format”:{},”standalone”:{}},”monthsCache”:{“format”:{},”standalone”:{}},”meridiemCache”:null,”eraCache”:{},”specifiedLocale”:null,”fastNumbersCached”:null},”invalid”:null,”weekData”:null,”localWeekData”:null,”c”:{“year”:2024,”month”:8,”day”:29,”hour”:8,”minute”:12,”second”:58,”millisecond”:667},”o”:120,”isLuxonDateTime”:true}
I guess this is expected "isLuxonDateTime":true
. Why is state.startDate instanceof DateTime
returning false? When I check in the redux dev tools, its not deserialzed or something
console.log(state.startDate.plus({days: action.payload }))
finally throws an error
Uncaught TypeError: state.startDate.plus is not a function
What am I doing wrong here?