I want to use https://github.com/t1m0n/air-datepicker with in a React app, but it doesn\'t work.
First of all air-datepicker
is not React component, it's jQuery plugin, so it won't work like in your examples.
Secondly, for proper work, jQuery should be availabel in global context, the most esiest ways to do this is to include it into page via tag. It should be included before plugin script.
To check if its really included and it available globally, just type in Chrome console window.jQuery
and press Enter it should return something like function (a,b){...}
. If not, when you doing something wrong, check src
attribute of tag, maybe it pointing to wrong destination
How to get it worked in React?
Well, the most simpliest way is just to initialize plugin on any DOM element in your component, like in regular JavaScript.
class MyComponent extends React.Component {
componentDidMount(){
this.initDatepicker();
}
initDatepicker(){
$(this.refs.datepicker).datepicker();
}
render(){
return (
Choose date!
)
}
}
ReactDOM.render(
,
document.getElementById('app')
);
As separate component
Very simple datepicker React component example
class Datepicker extends React.Component {
componentDidMount(){
this.initDatepicker();
}
initDatepicker(){
$(this.refs.datepicker).datepicker(this.props);
}
render(){
return (
)
}
}
class MyComponent extends React.Component {
render(){
return (
Choose date from React Datepicker!
)
}
}
ReactDOM.render(
,
document.getElementById('app')
);
Also don't forget to include css files.