How can I use date component in a React form

212 views Asked by At

I created a form and it has a date-picker field. I want to use the date-picker component but unable to figure out how to get it work. I'm using the cdn instead of npm and not sure if I'm missing something critical.

class Request extends React.Component{
  constructor(){
    super()
    this.state = {
      location:'',
      date: ''
    }
    this.handleLocation = this.handleLocation.bind(this)
  }
  handleDateSelect(e){
     this.setState({
      date: e.target.value
    })   
  }
  handleLocation(e){
    this.setState({
      location: e.target.value
    })
  }
  handleDateChange(e){
    this.setState({
      date: e.target.value
    })
  }
  render(){
    return(
      <div>
          Today's Date:  <DatePicker selected={this.state.date} onSelect={this.handleDateSelect} onChange={this.handleDateChange} /><br />
          Location: <input type="text" name="location" onChange={this.handleLocation} value={this.state.location} />
        {this.state.location}
      </div>
    )    
  }
}

The error that I get is Uncaught ReferenceError: useState is not defined Here's a pen for more context

1

There are 1 answers

9
Yannick Vermeulen On

Have you imported useState?

import React, { useState } from 'react';