I'm using the react-datepicker npm module and its styles got broke (styles are not being applied) when I deploy the build, it's working fine in the local environment.
I've imported its styles like this: import 'react-datepicker/dist/react-datepicker.css';
I found somewhere to import like this: import './../../node_modules/react-datepicker/dist/react-datepicker.css';
It's also not working.
I thought this could be the reason because of SSR so I removed SSR for this component but had no luck with CSR as well.
Current Result
Desired Result
Here is my Component code:
import React from 'react';
import DatePicker from 'react-datepicker';
import calendarIcon from './../../assets/images/calendar-icon.svg';
import 'react-datepicker/dist/react-datepicker.css';
// import 'react-datepicker/dist/react-datepicker-cssmodules.min.css';
import './Datepicker.scss';
const Datepicker = ({ datepickerClassName, datepickerStyle,
selectedDate, datepickerInputClassName, handleChange }) => (
<div className={`datepicker d-flex align-center
${!!datepickerClassName ? datepickerClassName : ''}`}
style= {datepickerStyle}
>
<span className='d-flex align-center icon-container'>
<img src={calendarIcon} className='icon' />
</span>
<DatePicker
placeholderText='DD/MM/YYYY'
dateFormat='dd/MM/yyyy'
id='start-date'
autoComplete='off'
selected={selectedDate}
className={datepickerInputClassName}
onChange={handleChange}
/>
</div>
);
export default Datepicker;
Please help me with this if someone faced this issue or has some idea about this.
The React datepicker is a compelling and reusable component used for displaying the dates using a calendar dialog. There are lots of datepicker packages available, but we are going to use the React Date Picker package to display date and time in a React application.
The React datepicker is a compelling and reusable component used for displaying the dates using a calendar dialog. There are lots of datepicker packages available, but we are going to use the React Date Picker package to display date and time in a React application.
Also, if you can import DatePicker, can't you use ES6 modules to import 'react-datepicker/dist/react-datepicker.css' in your setup @ffpetrovic? :) Sorry, something went wrong. Yes, if you can import the component using ES6 import then it should work for .css files as well.
The selected props take a date-format value to represent the value of this component, while onChange will act as a handler function to change the selected value. The onChange function takes a date as an argument and the only thing we need to do on this function is to change the value of the selected date.
Add its style file by
import 'react-datepicker/dist/react-datepicker.css'
You can use CDN, to solve the issue
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/react-datepicker/2.14.1/react-datepicker.min.css" />
In Next js, if you want to import css of a Library, use the CDN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With