问题
I would like to use the Pikaday datepicker with ReactJS. It is not based on ReactJS. How can I use it?
回答1:
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
});
}
回答2:
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
来源:https://stackoverflow.com/questions/30058477/how-can-i-use-pikaday-with-reactjs