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.
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 });
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