React Datepicker( can't get value of input)

前端 未结 7 1701
名媛妹妹
名媛妹妹 2021-02-20 06:25

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 vari

7条回答
  •  时光取名叫无心
    2021-02-20 07:01

    If you want to get the new value (once the user clicks on a new date from DatePicker) pass the event to the method.

    class MyComponent extends React.Component {
      constructor(props) {
        this.state = {inputValue: moment(),} // this will set the initial date to "today", 
                                             // you could also put another prop.state value there
        this.handleChange = this.handleChange.bind(this);
      }
      }
    
    
      handleChange(value) {
        console.log(value); // this will be a moment date object
        // now you can put this value into state
        this.setState({inputValue: value});
      }
    
      render(){
        ...
         this.handleChange(event)}
           selected={this.state.inputValue} otherProps={here} />
        ...
      }
    };
    

提交回复
热议问题