React Datepicker(无法获取输入值) [英] React Datepicker( can't get value of input)

查看:20
本文介绍了React Datepicker(无法获取输入值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 React 新手.我需要使用

解决方案

就用这个:

handleChange = date =>{const valueOfInput = date.format();///...};

因为这个 datepicker 返回一个 moment.js 对象!

有关更多信息,请查看 moment.js 此处的文档.

I am new in react. I need use react-datepicker

I want to get value of input, when I change date. If i click on 20th October 2017, i want put 20th October 2017 in my variable. But the main problem that I should work with component, not with input.

Before I just took value from state. Like this.state.value. But right now it is object(Moment) in state. And this object doesn't have value field.

There is my code:

export default class DatePicker extends Component {
constructor (props) {
    super(props);
    // this.props.date should looks like "29 November 2017 00:00"
    // second argument for moment() it is format of date, because RFC 2822 date time format
    this.state = {
        date: moment(this.props.value, 'LLL')
    };
}
handleChange = (date) => {
  // const valueOfInput = this.state.date  <--- I want string with date here
  console.log('this.state.date',this.state.date);
  this.setState({date: date});
};
render() {
    return <Form.Field>
              <Label>
                <Picker
                  selected={this.state.date}
                  onChange={this.handleChange}
                  dateFormat="LLL"
                  locale={this.props.language}
                />
              </Label>
          </Form.Field>

解决方案

Just use this:

handleChange = date => {
  const valueOfInput = date.format();
  ///...
};

Because this datepicker returns a moment.js object!

For more information, look into the moment.js docs here.

这篇关于React Datepicker(无法获取输入值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆