当我尝试在组件中初始化状态时,它会给我 SyntaxError:
处的意外标记 constructor(props) {
super(props);
this.state = {
isFromDatePicked = false,
isToDatePicked = false,
markedDates = {},
}
}
isFromDatePicked = false
行。我还尝试在构造函数之外初始化状态,但也根本不起作用。
此外,当我删除状态初始化以测试代码时,它会给我同样的错误
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
行。
我不知道为什么会抛出这样的错误。任何帮助将不胜感激,谢谢。
最佳答案
在 {}
中使用 :
而不是 =
。来自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
在 state
之前也使用 this
constructor(props) {
super(props);
this.state = {
isFromDatePicked:false,
isToDatePicked:false,
markedDates:{},
}
}
而且在 setState
中,您正在传递一个对象,因此请使用 :
而不是 =
this.setState({ isFromDatePicked: true, isToDatePicked: true, markedDates :_markedDates });
关于javascript - 在构造函数中初始化状态时出现意外标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55631196/