I would like to use the Pikaday datepicker with ReactJS. It is not based on ReactJS. How can I use it?
Asked
Active
Viewed 2,813 times
2 Answers
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
});
}

Cymen
- 14,079
- 4
- 52
- 72
-
1By the way, if you like Pikaday and want something similar in React (but not quite exactly the same), I made this: https://www.npmjs.com/package/react-daypicker – Cymen Apr 20 '18 at 01:10
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

ak85
- 4,154
- 18
- 68
- 113