4

I would like to use the Pikaday datepicker with ReactJS. It is not based on ReactJS. How can I use it?

2 Answers 2

5

To use Pikaday with ReactJS you can require it as you would any other node module. The trick is to hook into the React rendering event so that after the view is rendered, Pikaday is initialized on any rendered DOM inputs you want to use it on. You can do this with the ReactJS lifesystle event componentDidRender:

  componentDidMount: function() {
    new Pikaday({
      field: React.findDOMNode(this.refs.start),
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeStart
    });

    new Pikaday({
      field: React.findDOMNode(this.refs.end),
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeEnd
    });
  }
1
1

To get this working in React 16.3 I had to update what you had to the below.

import React from 'react'
import Pikaday from 'pikaday'

class PikadayWrap extends React.Component {
  constructor (params) {
    super(params)
    this.myRef = React.createRef()
  }

  componentDidMount () {
    new Pikaday({
      field: this.myRef.current,
      format: 'MM/DD/YYYY',
      onSelect: this.onChangeStart
    })
  }

  render () {
    return <div>
      <input type='text' ref={this.myRef} />
    </div>
  }
}
export default PikadayWrap

See this question for more info.

How to access a DOM element in React? What is the equilvalent of document.getElementById() in React

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.