3

Here is the component:

import React, { Component } from 'react';
import DatePicker from 'react-datepicker';

class DatePickerCreater extends Component {
    constructor(props){
        super(props);
    }

    render() {
        return (
            <DatePicker
                disabled={this.props.answer.isDisabled}
                dateFormat="YYYY/MM/DD"
                selected={Date(this.props.answer.value)}
                onChange={(e) => this.props.blurHandler(e.target.value,this.props.answer)} 
            />
        );
    }
}

export default DatePickerCreater

when i try to render it, it shows nothing...

Very grateful for every answer!

trixn
  • 15,761
  • 2
  • 38
  • 55
LittleMygler
  • 632
  • 10
  • 24
  • I can only see a `jsx` snippet. Where is the component? – trixn Aug 06 '18 at 15:05
  • the jsx snippet is the whole component. I've imported the component from 'react-datepicker' so it's not my component, I've cut out the rest of the code the class defenition and export just to make it easier – LittleMygler Aug 06 '18 at 15:06
  • 1
    Then please do not omit crucial parts of your component. The problem may be in that part. You can leave out imports and exports if you like to keep it simple. – trixn Aug 06 '18 at 15:08
  • I know that dude... as I wrote before, I did just put the relevant information here. There's nothing wrong with my component, only this part. I'll add some example code so you understand how it looks partly – LittleMygler Aug 06 '18 at 15:09
  • You say it doesn't render so maybe there is something wrong. So please if you post a component, post the whole code so that others can see if there is something wrong or not. – trixn Aug 06 '18 at 15:10
  • If that is your `constructor` it will not work. The constructor takes `props` as an argument and calls `super()` with it. – trixn Aug 06 '18 at 15:15
  • Is there any error in your console or does it just not render? – trixn Aug 06 '18 at 15:18
  • It just doesn't render.. :/ – LittleMygler Aug 06 '18 at 15:19
  • Can you confirm, that your `DatePickerCreater` component is actually getting rendered by its parent? Because there seems to be nothing wrong except that you need to pass a moment.js date as `selected` prop. – trixn Aug 06 '18 at 15:25
  • yeah I saw that too, and i can confirm that it is getting rendered, because I can see the div that has built there and also if I add random things they are shown – LittleMygler Aug 06 '18 at 15:29
  • If you wrap the contents of your `return` with a div and put other content in that div as a sibling to your datepicker, do you see that content? – mccambridge Aug 06 '18 at 15:31
  • Did you forget to import the css file? See my updated answer. – trixn Aug 06 '18 at 15:32
  • 1
    How are you compiling your output code? Can you confirm that you are running the correctly compiled code? What is the div that has been built? How do you know it is the date picker component that has rendered this? Install React dev tools to chrome and get some more insights into what is going on. – Tim B James Aug 06 '18 at 15:34
  • 2
    I created a codesandbox example with your code and it renders correctly: https://codesandbox.io/s/4zj6j0pvq7. Only difference is I imported the css file. Don't know if you did. – trixn Aug 06 '18 at 15:36
  • By doesn't render you mean there is nothing there when you inspect the page, i.e. no elements of that component are rendered? Or do you mean it doesn't visibly show up? – Dominic Aug 06 '18 at 15:36
  • Added the css file but nothing changed.. I Will check the data coming from the api tomorrow, thanks for all the responses wow guys! I’ll be in touch tomorrow when I get back into this. Will check with React developer tools for chrome I have it, but haven’t learned how to use it yet.. – LittleMygler Aug 06 '18 at 15:38
  • Also just follow some very basic steps. Change to just returning `

    Hello World

    ` if that doesn't render, then you have bigger issues.
    – Tim B James Aug 06 '18 at 15:48

2 Answers2

14

The selected property takes a moment.js date, not a plain javascript date. You should get an error that says "date.clone is not a function ". You can fix that by using a moment date:

import moment from 'moment';

class DatePickerCreater extends Component {
    render() {
        return (
            <DatePicker
                disabled={this.props.answer.isDisabled}
                dateFormat="YYYY/MM/DD"
                selected={moment(this.props.answer.value)}
                onChange={(e) => this.props.blurHandler(e.target.value, this.props.answer)}
            />
        )
    }
}

Also you may have forgotten to import the react-datepicker css file. Without that the popup will not show correctly:

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

EDIT

Since they switched to using date-fns this answer is outdated. If you are using react-date-picker with a version >=2.0 you can no longer pass a moment.js instance. Instead use native Date objects and manipulate them using date-fns.

For Reference:

Up until version 1.8.0, this package was using Moment.js. Starting v2.0.0, we switched to using date-fns, which uses native Date objects, to reduce the size of the package. If you're switching from 1.8.0 to 2.0.0 or higher, please see the updated example above of check out the examples site for up to date examples.


trixn
  • 15,761
  • 2
  • 38
  • 55
0

The code is not enough, what can I tell super() need to be called with props:

constructor(props){
   super(props);
   // ... code
}
Mosè Raguzzini
  • 15,399
  • 1
  • 31
  • 43
  • yeah, fixed that, but doesn't solve my problem :/ What more do you need? Tell me and I'll add it. What I can see, this is the only thing relevant for the datepicker component, isn't it? – LittleMygler Aug 06 '18 at 15:18
  • Show me how do you render class DatePickerCreater in its parent – Mosè Raguzzini Aug 06 '18 at 15:19