I use Date Picker component from Material-UI for React JS. I want to show the selected date on the table. A date is an object and I have an error when trying to show in a table row. How to do this?
import React, { Component } from 'react';
import DatePicker from 'material-ui/DatePicker';
import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';
export default class AddTaskDialog extends Component {
  constructor(props) {
    super(props);
    this.state = { controlledDate: {} };
    this.handleChangeDate = this.handleChangeDate.bind(this);
  }
  handleChangeDate = (event, date) => {
    this.setState({
      controlledDate: date,
    });
  };
  render() {
    return (
      <div>
        <DatePicker hintText="Date" value={this.state.controlledDate} onChange={this.handleChangeDate}/>
        <Table>
          <TableHeader>
            <TableRow>
              <TableHeaderColumn>Date</TableHeaderColumn>
            </TableRow>
          </TableHeader>
          <TableBody>
            <TableRow>
              <TableRowColumn>{this.state.controlledDate}</TableRowColumn>
            </TableRow>
          </TableBody>
        </Table>
      </div>
    );
  }
}
The date that is sent to the handleChangeDate handler is of type object. You need to convert it to a date string in order to render inside the TableRowColumn.
export default class AddTaskDialog extends Component {
 constructor(props) {
 super(props);
 this.state = { controlledDate: new Date() };
 this.handleChangeDate = this.handleChangeDate.bind(this);
}
handleChangeDate = (event, date) => {
  this.setState({
    controlledDate: date
  });
};
// INSIDE RENDER
<TableRowColumn>{this.state.controlledDate.toDateString()}</TableRowColumn>
const date = new Date();
console.log(typeof date);
   
console.log(date.toDateString());If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With