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());
You are missing the state in the TableRow.
constructor(props) {
super(props);
this.state = { controlledDate: '' };
this.handleChangeDate = this.handleChangeDate.bind(this);
}
...
render() {
...
<TableRow>
TableRowColumn>{this.state.controlledDate || New Date(Date.now())}</TableRowColumn>
</TableRow>
}
{this.state.controlledDate}
不是{this.controlledDate}
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.