最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unable to get Material-Ui datepicker date on the first change - Stack Overflow

programmeradmin4浏览0评论

So I am unable, to get date from material-ui dateandtimepicker on the first select, somehow, i get value only on second click, and that value is previous one, Also would love to know if there is any other way to convert date to format like this yyyy:mm:dd:hh:mm without using moment.js

Here is my code:

import React, { Component } from 'react'
import DataPicker from './UI-ponents/DataPicker'

class EventForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      errors: {},
      start:'',
      end:''
    }
  }

  onChange1(e) {
    this.setState({
      start: e.target.value,
    });
    console.log(this.state.start)
  }

  render() {
    return (
      <div>
        <DataPicker
          label="Event Starts"
          onChange={this.onChange1.bind(this)} 
          defaultas="2017-05-24T10:30"
        />
      </div>
    )
  }
}
export default EventForm;

DatePicker.js

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 300,
  },
});

function DateAndTimePickers(props) {
  const { classes } = props;

  return (
    <form className={classes.container} noValidate>
      <TextField
        id="datetime-local"
        label={props.label}
        type="datetime-local"
        defaultValue={props.defaultas}
        className={classes.textField}
        onChange = {props.onChange} 
        value = {props.value} 
        InputLabelProps={{
          shrink: true,
        }}
      />
    </form>
  );
}

DateAndTimePickers.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DateAndTimePickers);

So I am unable, to get date from material-ui dateandtimepicker on the first select, somehow, i get value only on second click, and that value is previous one, Also would love to know if there is any other way to convert date to format like this yyyy:mm:dd:hh:mm without using moment.js

Here is my code:

import React, { Component } from 'react'
import DataPicker from './UI-ponents/DataPicker'

class EventForm extends Component {
  constructor(props) {
    super(props);
    this.state = {
      text: '',
      errors: {},
      start:'',
      end:''
    }
  }

  onChange1(e) {
    this.setState({
      start: e.target.value,
    });
    console.log(this.state.start)
  }

  render() {
    return (
      <div>
        <DataPicker
          label="Event Starts"
          onChange={this.onChange1.bind(this)} 
          defaultas="2017-05-24T10:30"
        />
      </div>
    )
  }
}
export default EventForm;

DatePicker.js

import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from '@material-ui/core/styles';
import TextField from '@material-ui/core/TextField';

const styles = theme => ({
  container: {
    display: 'flex',
    flexWrap: 'wrap',
  },
  textField: {
    marginLeft: theme.spacing.unit,
    marginRight: theme.spacing.unit,
    width: 300,
  },
});

function DateAndTimePickers(props) {
  const { classes } = props;

  return (
    <form className={classes.container} noValidate>
      <TextField
        id="datetime-local"
        label={props.label}
        type="datetime-local"
        defaultValue={props.defaultas}
        className={classes.textField}
        onChange = {props.onChange} 
        value = {props.value} 
        InputLabelProps={{
          shrink: true,
        }}
      />
    </form>
  );
}

DateAndTimePickers.propTypes = {
  classes: PropTypes.object.isRequired,
};

export default withStyles(styles)(DateAndTimePickers);
Share Improve this question edited Jul 13, 2018 at 12:19 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Jul 13, 2018 at 12:14 Tautvydas BūdaTautvydas Būda 2141 gold badge5 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

You are not passing down the value to the DatePicker ponent. You could use start as value to control the DatePicker and ignore the defaultValue entirely.

The reason why console.log(this.state.start) in your onChange1 handler isn't displaying the start you would think is because setState is asynchronous, which means this.state.start will not have been updated yet.

class EventForm extends Component {
  state = {
    text: '',
    errors: {},
    start: '2017-05-24T10:30',
    end: ''
  };

  onChange1 = (e) => {
    this.setState({
      start: e.target.value,
    });
  };

  render() {
    return (
      <div>
        <DataPicker
          label="Event Starts"
          onChange={this.onChange1}
          value={this.state.start}
        />
      </div>
    )
  }
}

i don't know much about react but this might help with the formatting of the dates;

<td>
{new Intl.DateTimeFormat('en-GB', { 
    year: 'numeric', 
    month: 'long', 
    day: '2-digit' 
}).format(customer.firstSale)}
</td>

Example from here;

Well you are getting old one means last set value in state cause you are doing things simultaneously/parallelly both set value and console value. Means you know JS is async you are doing here

 onChange1(e) {
    this.setState({
      start: e.target.value,
    });
    console.log(this.state.start)
  }

what is happening here setting the new value to state and console current/last/default(first time) state value, That's why you are getting it the second time.

to get current one do like this:

onChange1(e) {
        this.setState({
          start: e.target.value,
        },function(whatever){
          console.log(this.state.start)
        });
     }

it will execute and console value once your set state is done.

And for formatting date without momentjs you can find the source of momentjs what they are doing under the hood obviously they are using JS. If I were you I'll do this way. Haha

发布评论

评论列表(0)

  1. 暂无评论