react datetimepicker with meteor - reactjs

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.

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.

Related

how to add a react date picker no hooks

I am working on a react project trying to add a react date picker using npm react-datepicker. I am using this.state and handleChange(e). how do I add a date picker on my react app without using hooks?
here is sample of the connection, in App component, its old way of doing date picker using in reactjs,
your imports are,
import React, { Component } from 'react';
import DatePicker from 'react-datepicker';
and application would be,
class App extends Component {
constructor (props) {
super(props)
this.state = {
startDate: new Date()
};
this.handleChange = this.handleChange.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
}
handleChange(date) {
this.setState({
startDate: date
})
}
onFormSubmit(e) {
e.preventDefault();
console.log(this.state.startDate)
}
render() {
return (
<form onSubmit={ this.onFormSubmit }>
<div className="form-group">
<DatePicker
selected={ this.state.startDate }
onChange={ this.handleChange }
name="startDate"
dateFormat="MM/dd/yyyy"
/>
<button className="btn btn-primary">Show Date</button>
</div>
</form>
);
}
}
export default App;

REACT - Why isnt my Filter Method Working

Im trying to simply create a search and results feature for an app. The value of the input should reflect the components listed in the CardList Array. The filter doesn't seem to update the CardList. I've logged steps along the way and I've come to the conclusion that its the filter I set up. I cant seem to figure out why it wont filter the list.
import React, {Component} from 'react';
import CardList from './CardList';
import {robots} from './robots';
import './index.css';
class App extends Component {
constructor() {
super()
this.state = {
robots: robots,
searchfield: ''
}
}
onSearchChange = (event) => {
this.setState({ searchfield: event.target.value });
}
render() {
const filteredRobots = this.state.robots.filter(robot => {
return robot.name.toLowerCase().includes(this.state.searchfield.toLowerCase());
});
return (
<div className="appAlign">
<h1 className="appTitle">RoboFriends</h1>
<input
className="searchBox"
type="search"
placeholder="Search Robots"
onChange={this.onSearchChange}
/>
<CardList robots={filteredRobots} />
</div>
);
}
}
export default App;
The error is not caused by the filter function as I have tested it and it works. It most probably lies with the robots data-set. I have slightly modified the filter function here.
import React, { Component } from "react";
import CardList from "./CardList";
import { robots } from "./robots";
class App extends Component {
constructor() {
super();
this.state = {
robots: robots,
searchfield: ""
};
}
onSearchChange = event => {
this.setState({ searchfield: event.target.value });
};
render() {
const filteredRobots = this.state.robots.filter(robot =>
robot.name.toLowerCase().includes(this.state.searchfield.toLowerCase())
);
return (
<div className="appAlign">
<h1 className="appTitle">RoboFriends</h1>
<input
className="searchBox"
type="search"
placeholder="Search Robots"
onChange={this.onSearchChange}
/>
<CardList robots={filteredRobots} />
</div>
);
}
}
export default App;
I have made a sandbox with your code which has a sample robots data and a Card that renders the filtered data-set. Take a look.

I am getting TypeError: this is undefined even after using this.handleChange = this.handleChange.bind(this) in constructor

I am getting an "TypeError: this is undefined" error even after I am binding this to the function in the constructor. I have also tried to use handleChange={this.handlChange.bind(this)} instead of this.handleChange = this.handleChange.bind(this) but it doesn't work. I am watching React tutorial and I wrote the same code as the tutor did, the code is working fine in his IDE but is showing error in mine.
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
ReactDOM.render(<App />, document.getElementById('root'));
App.js
import React from 'react';
import './css/App.css';
import Header from "./components/Header"
import MainContent from "./components/MainContent"
import Footer from "./components/Footer"
class App extends React.Component {
render(){
return (
<div>
<Header />
<MainContent />
<Footer />
</div>
)
}
}
export default App;
MainContent.js
import React, {Component} from "react"
import ListItem from "./ListItem"
import tasks from "./tasks"
class MainContent extends Component
{
constructor()
{
super()
this.state =
{
tasks: tasks
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(completed)
{
alert(completed)
}
render()
{
const taskComponents = this.state.tasks.map(function(data)
{
return <ListItem key={data.id} task={data.task} completed={data.completed} handleChange={this.handleChange} />
})
return (
<div>
{taskComponents}
</div>
)
}
}
ListItem.js
import React from "react"
let i=1;
class ListItem extends React.Component
{
render()
{
return (
<div>
<p style={{color : i%2 ? "black" : "orange"}}>
<input type="checkbox" checked={this.props.completed}
onChange={function(){
this.props.handleChange(this.props.completed)
}}>
</input> Task {i++} : <b>{this.props.task}</b>
</p>
</div>
)
}
}
export default ListItem

ReactJS DatePicker stops working after lifting state up

ReactJS DatePicker works fine when I keep the state inside Datepicker itself, however once I lift the state up it stops working - nothing happening when I select a date.
This code works:
import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
class Datepicker extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
date: date
});
}
render() {
return <DatePicker selected={this.state.date} onChange={this.handleChange} />;
}
}
class Index extends React.Component {
render() {
return (
<div>
<Datepicker />
</div>
);
}
}
ReactDOM.render(<Index />, document.getElementById("root"));
But this code does not work - nothing happens when I select a date in the datepicker:
import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
class Datepicker extends React.Component {
render() {
return (
<DatePicker
selected={this.props.date}
onChange={() => {
this.props.handleChange(this.props.date);
}}
/>
);
}
}
class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
date: date
});
}
render() {
return (
<div>
<Datepicker date={this.state.date} handleChange={this.handleChange} />
</div>
);
}
}
ReactDOM.render(<Index />, document.getElementById("root"));
I also uploaded code example here (index.js is not working as expected while index2.js is working).
Would be very grateful if anyone could advise how to lift the state up and at the same moment have Datepicker working.
The issue is you are passing the this.props.date into the handleChange function which never changes.
You can simply remove the arrow function and just call this.props.handleChange without passing it any parameters. It should automatically choose the new date value selected.
import React from "react";
import ReactDOM from "react-dom";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
const Datepicker = props => (
<DatePicker selected={props.date} onChange={props.handleChange} />
);
class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
date: new Date()
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
date: date
});
}
render() {
return (
<Datepicker
date={this.state.date}
handleChange={this.handleChange}
/>
);
}
}
ReactDOM.render(<Index />, document.getElementById("root"));
See a working example: https://codesandbox.io/s/my4zp6rj8y

can i pass a state value with the jsx code ?? in reactjs

Hello everybody I'm wondering if I can pass a state value from a component to other where I'm returning jsx code to be displayed for example I have 3 components.
1
import React, { Component } from 'react';
import Conteneur from './Conteneur';
class Header extends React.Component {
constructor(props) {
super(props);
this.state = { value: '' };
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
this.setState({ value: event.target.value });
}
handleSubmit(event) {
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
<Conteneur values={this.state.value} />
</form>
);
}
}
export default Header;
2 app.js
import React, { Component } from 'react';
import Header from './Header';
import Conteneur from './Conteneur';
import './App.css';
class App extends Component {
render() {
return (
<div className="App" >
<br />
<Header />
<br />
<Conteneur />
</div>
);
}
}
export default App;
3 and finally
import React, { Component } from 'react';
const Conteneur = () => {
return (
<div className="tab"><span>ok test </span></div>
);
};
export default Conteneur;
I like to pass the state value of header that I have from the input to conteneur and then display in the box while I have some code all the examples that I saw online they are sending state like this:
class Dashboard extends Component {
...
...
render(){
return(
<Sidebar data={this.state.data1}/>
);
}
}
So can I do like this <Conteneur values={this.state.value} /> in the form ?
And I imported Conteneur.
i updated the code but the output is
Yes you can do, only one thing you are missing. Receive the props in the function parameters then render that in the ui.
Like this:
const Conteneur = (props) => {
return (
<div className="tab"><span>value: {props.value} </span></div>
);
};

Resources