简体   繁体   中英

Antd Timepicker set defaultValue from variable

As per their Timepicker library i did following in React:

<TimePicker defaultValue={moment(dbTime1, format)} format={format}
onChange={time => setStartTime(moment(time).format(format))}/>

the state is:

const [dbTime1, setDbTime1] = useState("");
const [startTime, setStartTime] = useState(new Date());

Reading from database the string value 09:00 with:

setDbTime1(moment(timeWindowsData.din_times_for_rep[0].val));

The problem that the visual part is not displaying required to me 09:00 time, it just offering to select a time. If I do some modifications to the formats, etc - it gaves me various errors. Starting from invalid date and finishing with ipossibility to select a time, falling back to Date(), etc.

How can I store the variable and display the time in time field as default value?

It's working if i just drop the string there:

<TimePicker defaultValue={"09:00", format)} format={format}
onChange={time => setStartTime(moment(time).format(format))}/>

But I need that it must read from variable.

I removed this component and used the other ones.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM