Home > database >  event.target.value for datetime piker
event.target.value for datetime piker

Time:02-04

How do I set a react state with a event.target.value in a datetime piker?

Here is my code that is not working:

const [ date, setDate ] = useState(new Date());


<input type="datetime-local" className="form-control" onChange={event => setDate(event.target.value)} value={date}/>   

Here is the error that shows me:

Argument of type 'string' is not assignable to parameter of type 'SetStateAction'.ts(2345)

Do you know how can I fix this?

CodePudding user response:

<input> expects the type of provided data to be string, currently it is of type Date.

Also, it expects the date to be of the format YYYY-MM-DDThh:mm. You can update the state initialization as:

 const [date, setDate] = useState<string>(moment().format("YYYY-MM-DDThh:mm"));
  •  Tags:  
  • Related