Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-datepicker styles are not being not applied on deployed build

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

enter image description here

Desired Result

enter image description here

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.

like image 516
Jappreet Avatar asked Nov 20 '19 13:11

Jappreet


People also ask

What is a datepicker in react?

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.

How 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.

Can I use ES6 modules to import datepicker in react-datepicker?

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.

What is the difference between onchange and selected props in react?

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.


Video Answer


2 Answers

Add its style file by

import 'react-datepicker/dist/react-datepicker.css'
like image 181
saeed eivazi Avatar answered Oct 23 '22 15:10

saeed eivazi


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

like image 13
Clement Olaniyan Avatar answered Oct 23 '22 15:10

Clement Olaniyan