I had jquery-2.2.x
earlier and code was running fine. Now, I have updated the jquery-3.3.x
and started getting the error datepicker()
function doesn't exist. After searching about that error, came to know that it need moment.js
and bootstrap.js
. So after adding these package, I am getting the error :
Error: Bootstrap's JavaScript requires jQuery
Although I'm importing the jquery
.
This code is running without any error when I am using jquery-2.2.x
, but the same code with jquery-3.3.x
is not working and giving error datepicker()
function is not defined.
const React = require('react');
const $ = require('jquery');
require('bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css');
require('bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js');
require('styles/InputField.css');
/*
Renders a single input field with label and inputBox
Attributes
1)labelText -> text for the label
2)inputId -> Id for the input box
3)inputPlaceholder -> placeholder for the input
4)isDate -> if the field is for date activate the jquery datepicker
Returns a div
*/
class InputField extends React.Component {
constructor(props) {
super(props);
this.state = {
datepickerEnabled: false
}
}
// If the component is mounted and it is a date field activate the datepicker
componentDidMount() {
if(this.props.isDate === 'true') {
$('#' + this.props.inputId).datepicker({dateFormat: 'mm/dd/yy'});
this.setState({
datepickerEnabled: true
});
}
}
render() {
return (
<div>
<label htmlFor={this.props.inputId}> {this.props.labelText} </label>
<input type='text' id={this.props.inputId} placeholder={this.props.inputPlaceholder} className='form-control'/>
</div>
);
}
}
module.exports = InputField;