简体   繁体   中英

react-dates broken - not recognising css?

I have created a simple datepicker component based on react-dates and implemented as per documentation at https://github.com/airbnb/react-dates

The datepicker seems to be working however the calendar styling is completely broken when clicking on the datepicker field

The datepicker code:

import React, { Component } from 'react';
import moment from 'moment';
import 'react-dates/initialize';
import { SingleDatePicker } from 'react-dates';
import 'react-dates/lib/css/_datepicker.css';

export default class DatePicker extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedStartDate: moment(),
            calendarSelectedStartDate: false
        }
    }
    
    onDateChange = (selectedStartDate) => {
      this.setState(() => ({ selectedStartDate }));
    };
    onFocusChange = ({ focused }) => {
      this.setState(() => ({ calendarSelectedStartDate: focused }));
    };

    render() {
        return (
            
          <SingleDatePicker
            date={this.state.selectedStartDate}
            onDateChange={this.onDateChange}
            focused={this.state.calendarSelectedStartDate}
            onFocusChange={this.onFocusChange}
            numberOfMonths={1}
            isOutsideRange={() => false}
          />
        )
    }
}

Implementation is just call to:

<DatePicker />

outside of any parent html tags that could affect it.

The styling looks like this:

在此处输入图像描述

Ok i found an answer for this problem, so:

Are you trying to render the calendar inside another element that is not always visible, right?

Well, if you are doing that, in the parent component you must have an state like "isOpen" or something like that. Then, when you call the Picker component, must be like:

{isOpen && <DatePicker />}

I spent like two hours searching this solution. I hope that make sense for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM