0

Hi I need help to properly write the code full working for Datepicker: https://reactdatepicker.com/ I have trouble importing the css and after spending too much time I decided to just grab the code. Please help make it work and forgive me as I am new to react. Thanks.

import DatePicker from "react-datepicker";

function DatePicker() {

    const [startDate, setStartDate] = useState(new Date());
    const MyContainer = ({ className, children }) => {

        return (
          <div style={{ padding: "16px", background: "#216ba5", color: "#fff" }}>
            <CalendarContainer className={className}>
              <div style={{ background: "#f0f0f0" }}>
                What is your favorite day?
              </div>
              <div style={{ position: "relative" }}>{children}</div>
            </CalendarContainer>
          </div>
        );
      };

      return (
        <DatePicker
          selected={startDate}
          onChange={(date) => setStartDate(date)}
          calendarContainer={MyContainer}
        />
      );
    };

export default DatePicker```
4
  • 1
    you have issue iwth css?.can you show how it looksn ow Commented Jun 5, 2021 at 4:39
  • I can't make this work: import 'react-datepicker/dist/react-datepicker-cssmodules.css'; I know there is thread out there for this but I already spent time without result so I decided to grab the code and manully add it but I am new to react and I don't know what I am lacking in the code above to work. Commented Jun 5, 2021 at 4:48
  • let me check once Commented Jun 5, 2021 at 4:50
  • thanks. I'm a total beginner so I would appreciate your help make it work. I grabbed it here: reactdatepicker.com Commented Jun 5, 2021 at 4:53

1 Answer 1

2

I have tested it.It loads fine.Here is the code

import React from 'react';
import DatePicker,{ CalendarContainer } from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";


function App() {
  const [startDate, setStartDate] = React.useState(new Date());
  const MyContainer = ({ className, children }) => {

    return (
      <div >
        <CalendarContainer className={className}>
          <div style={{ background: "#f0f0f0" }}>
            What is your favorite day?
              </div>
          <div style={{ position: "relative" }}>{children}</div>
        </CalendarContainer>
      </div>
    );
  };

  return (
    <DatePicker
      selected={startDate}
      onChange={(date) => setStartDate(date)}
      calendarContainer={MyContainer}
    />
  );
}

export default App;

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting an error: ./src/App.js Module not found: Can't resolve 'react-datepicker' in 'C:\Users\JD\Desktop\date-picker\src'
you should copy only inside code component not entire think .Since your component name is DatePicker but in my case its App so
I created a new react app. I figure out I missed to npm install react-datepicker --save let me check again

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.