简体   繁体   English

如何在 React 表单中使用日期组件

[英]How can I use date component in a React form

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.我正在使用 cdn 而不是 npm 并且不确定我是否遗漏了一些关键的东西。

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我得到的错误是Uncaught ReferenceError: useState is not defined这是更多上下文的

你导入了 useState 吗?

import React, { useState } from 'react';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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