Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token when initializing state in constructor

When i try to initialize state in a component it gives me SyntaxError: Unexpected token at

   constructor(props) {
    super(props);

    this.state = {
      isFromDatePicked = false,
      isToDatePicked = false,
      markedDates = {},
    }
  }

isFromDatePicked = false line. I also tried to initialize the state outside of the constructor but is not worked at all too.

Also, when i remove state initialization in order to test the code it gives me same error at

 let markedDates = { ...this.state.markedDates };
      let [_markedDates, range] = this.setupMarkedDates(this.state.fromDate, this.state.toDate, markedDates);
      if (range >= 0) {
        this.setState({ isFromDatePicked: true, isToDatePicked: true, markedDates = _markedDates }); 

markedDates = _markedDates line.

I have no idea why it is throwing such error. And any help will be appreciated, thanks.

like image 777
AlkanV Avatar asked Mar 05 '23 04:03

AlkanV


1 Answers

Use : instead of = inside {}. From the Object initializer

However, the advantage of the literal or initializer notation is, that you are able to quickly create objects with properties inside the curly braces. You simply notate a list of key: value pairs delimited by comma

Also use this before state

constructor(props) {
    super(props);

    this.state = {
      isFromDatePicked:false,
      isToDatePicked:false,
      markedDates:{},
    }
  }

And also in setState you are passing a object so use : instead of =

this.setState({ isFromDatePicked: true, isToDatePicked: true, markedDates :_markedDates });
like image 75
Maheer Ali Avatar answered Mar 12 '23 09:03

Maheer Ali