我是 react 新手。我需要使用react-datepicker
当我更改日期时,我想获取输入的值。 如果我点击 2017 年 10 月 20 日,我想将 2017 年 10 月 20 日放入我的变量中。 但主要问题是我应该使用组件,而不是输入。
在我刚刚从状态中获取值(value)之前。就像 this.state.value 一样。但现在它是处于状态的对象(Moment)。并且该对象没有值字段。
这是我的代码:
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>
最佳答案
只需使用这个:
handleChange = date => {
const valueOfInput = date.format();
///...
};
因为这个datepicker
返回一个moment.js
对象!
有关更多信息,请查看 moment.js docs here .
关于reactjs - React Datepicker(无法获取输入值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47159933/