Reputation: 377
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.
Upvotes: 28
Views: 31396
Reputation: 9
None of these solutions worked for me. I ended up getting it working by adding react-datepicker.js to my styles folder
Upvotes: 0
Reputation: 221
if you are using Next.js import
import "react-datepicker/dist/react-datepicker.css";
inside your _app.js page it will work.
Upvotes: 5
Reputation: 914
Add its style file by
import 'react-datepicker/dist/react-datepicker.css'
Upvotes: 23
Reputation: 36100
I had to import the styling inside application.scss
@import 'react-datepicker/dist/react-datepicker';
The reason the inline import (inside jsx/tsx) works in dev and not other environments is likely because in dev you have extract_css: false
(inside config/webpacker.yml
) and in other environments it's set to true
.
Upvotes: 12
Reputation: 423
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
Upvotes: 16