Search code examples
node.jsreactjstimepicker

Can't get the value from rc-time-picker


Basically this:

        <TimePicker
        id="startTime"
        showSecond={false}
        defaultValue={now}
        className="xxx"
        onChange={function(){
          console.log(this.value);
        }}
        format={format}
        use12Hours
        disabled={dateBlocked}
        name="startTime"
        />

is logging:

undefined

to the console. Why not it's date as a moment?

This

console.log(this.defaultValue);

Works fine.


Solution

  • Value

    The timePicker's value it's provide as an argument by theOnChange event.

    It should be:

     onChange={ function(value){console.log(value)} }
    

    Example

    // Display seconds
    const showSecond = true;
    
    //Format string
    const str = showSecond ? 'HH:mm:ss' : 'HH:mm';
    
    //Handle onChange event
    function handleChange(value) {
        console.log(value && value.format(str));
    }
    ...
    onChange={handleChange}
    

    github/example/picktime.js