3

I cannot make the component react-datepicker display properly.

it actually displays like this.

I wish it could display at least like the documentation for the component does.

I first thought it was a dependency problem, and added all the dependencies the doc says are needed. The result is still the same.

Some stackoverflow questions talked about this and referred to a missing stylesheet. However I imported everything with npm, so that shouldn't be the problem.

My class component looks like this :

import React from "react";
import "./style.css";
import DatePicker from "react-datepicker";

class Filters extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      startDate : new Date()
    };
    this.handleStartChange = this.handleStartChange.bind(this);
  }

  handleStartChange = (date) => {
    this.setState({
      startDate : date
    })
  }


  render() {
    return (
      <div className="filters">
          <div id="filterbox">
            <p id="titre">Filtres</p>
            <DatePicker 
              selected={this.state.startDate}
              onChange={this.handleStartChange} />
          </div>
      </div>
    )
  }
}



export default Filters;

I apologize in advance if the problem is very obvious, I'm quite new to reactjs.

3 Answers 3

13

You forgot to import the package css.

From the documentation:

import "react-datepicker/dist/react-datepicker.css";
Sign up to request clarification or add additional context in comments.

1 Comment

Aaaand such a beginners mistake. Thanks a lot.
0

I think you have to import the react-datepicker.css too, not only the package itself.

import "react-datepicker/dist/react-datepicker.css";

Below is a simple example of how to use the Datepicker in a React view. You will also need to require the CSS file from this package (or provide your own). The example below shows how to include the CSS from this package if your build system supports requiring CSS files (Webpack is one that does).

2 Comments

You sir, are right. Thank you. I will accept adesurirey's answer since it came just a few seconds before yours, but I'd accept both if I could.
No problem. I'm glad I could help. :)
0

you haven't imported their css file.

import "react-datepicker/dist/react-datepicker.css";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.