Reputation: 35
I am a newbie to meteor and react , sorry if this is a silly question.
I am trying to put a react datetimepicker in my meteor project without success.
below is the project structure ( same as the todo app in meteor's official guide)
imports
-ui
-App.jsx
-Example.jsx
code in Example.jsx (which I copied from the Controlled Component example in http://dev.quri.com/react-bootstrap-datetimepicker/]1
import React from 'react';
import DatePicker from 'react-datepicker';
import moment from 'moment';
import 'react-datepicker/dist/react-datepicker.css';
// CSS Modules, react-datepicker-cssmodules.css
// import 'react-datepicker/dist/react-datepicker-cssmodules.css';
class Example extends React.Component {
constructor (props) {
super(props)
this.state = {
startDate: moment()
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
startDate: date
});
}
render() {
return <DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
/>;
}
}
code in App.jsx
import React, { Component } from 'react';
import Example from './Example.jsx';
import DatePicker from 'react-datepicker';
import moment from 'moment';
export default class App extends Component {
render() {
return (
<div className="container">
<h1> Time selecting </h1>
<DatePicker selected={this.state.date} onChange={this.handleChange} />
</div>
)
};
}
My app can run and print the line "Time selecting" before I tried to add the datetimepicker and put the line
<DatePicker selected={this.state.date} onChange={this.handleChange} />
in App.jsx
How can I use the datetimepicker correctly?
Now I deleted the Example.jsx file and changed the file in App.jsx to
import React from 'react';
import DatePicker from 'react-datepicker';
import moment from 'moment';
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
date: "1990-06-05",
format: "YYYY-MM-DD",
inputFormat: "DD/MM/YYYY",
mode: "date"
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
startDate: date
});
}
render() {
return (
<div className="container">
<h1> Time selecting </h1>
<DatePicker selected={this.state.startDate}
onChange = {this.handleChange} />
</div>
)
}
}
but the browser is still showing nothing.
Upvotes: 0
Views: 622
Reputation: 1581
you probably just copied only the <DatePicker>
part you should also include the handleChange call back function and the initial state like below.
import React from 'react';
import DatePicker from 'react-datepicker';
import moment from 'moment';
class Application extends React.Component {
constructor(props) {
super(props);
this.state = {date: moment()};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
startDate: date
});
}
render() {
return <div className="container">
<h1> Time selecting </h1>
<DatePicker selected={this.state.date}
onChange={this.dateChanged} />
</div>
}
}
here is the codepen for react-datepicker demo.
Upvotes: 1