React native 如何解决';syntaxError意外标记,应为“}”&引用';
我正在用react native编写一个应用程序。这是我第一次这样做。我熟悉React native 如何解决';syntaxError意外标记,应为“}”&引用';,react-native,React Native,我正在用react native编写一个应用程序。这是我第一次这样做。我熟悉construct中的类和函数 到目前为止,一切都正常,但由于某种原因,代码没有编译并抛出此错误 语法错误:/ggProject/App.js:意外标记,应为“}” (119:23) 我已经有一段时间想弄清楚是什么错了,但我找不到错误。行119:23位于构造函数的第一个“{” class Cartas extends Component { constructor(props) { super(props);
construct
中的类和函数
到目前为止,一切都正常,但由于某种原因,代码没有编译并抛出此错误
语法错误:/ggProject/App.js:意外标记,应为“}”
(119:23)
我已经有一段时间想弄清楚是什么错了,但我找不到错误。行119:23
位于构造函数的第一个“{”
class Cartas extends Component {
constructor(props) {
super(props);
state = {
carta: 0
};
}
nextCard = () => {
var y = this.state.carta + 1;
this.setState({carta: y })
if(this.state.carta === 3) {Alert.alert('se activaron')}
}
displayCarta() {
if(this.state.carta <= 2){
if (this.state.carta%4 == 0) {
return <CTodos players={this.props.players} changePage={this.props.changePage} next={this.nextCard}/>
} else if (this.state.carta%4 == 1) {
return <CSecuencia changePage={this.props.changePage} next={this.nextCard} />
} else if (this.state.carta%4 == 2) {
return <CPregunta changePage={this.props.changePage} next={this.nextCard} />
}
}else {
if (this.state.carta%4 == 0) {
return <CTodos players={this.props.players} changePage={this.props.changePage} next={this.nextCard}/>
} else if (this.state.carta%4 == 1) {
return <CSecuencia changePage={this.props.changePage} next={this.nextCard} />
} else if (this.state.carta%4 == 2) {
return <CPregunta changePage={this.props.changePage} next={this.nextCard} />
}else if (this.state.carta%4 == 3) {
return <CReto changePage={this.props.changePage} next={this.nextCard} />
}
}
}
render() {
return (
this.displayCarta()
)
}
}
类Cartas扩展组件{
建造师(道具){
超级(道具);
状态={
卡塔:0
};
}
下一个卡片=()=>{
var y=this.state.carta+1;
this.setState({carta:y})
如果(this.state.carta==3){Alert.Alert('se activaron')}
}
displayCarta(){
如果(this.state.carta我认为您的组件应该是这样的
class Cartas extends Component
state = {
carta: 0
}
}
指针
您不使用构造函数进行初始化(至少在文档中没有指定这样做)
您不能通过此
设置状态。您可以像上面一样设置初始状态,然后再使用设置状态()
函数进行设置
根据提供的代码段判断,您没有添加结尾}
,这是一个语法错误。它应该如下所示:
class Cartas extends Component {
constructor(props) {
super(props);
this.state = {
carta: 0
};
}
}
在项目中添加lint工具和关系文本编辑器插件将帮助您实时发现这些语法错误。您确实需要显示更多的代码。这就是整个类,您还需要什么?:)调用构造函数并在主体中设置状态没有问题。OP的代码没有错误,按照他的方式编写代码也很好。您所编写的只是es6编写类似内容的方式。您还提供了语法错误的代码。我是新使用react native的,感谢您的更正。我一直在使用另一个类中的类结构和构造函数对我起作用。这就是为什么我觉得这个错误很奇怪。我正在用Expo在android设备上验证我的代码。可能这个错误与此有关。现在Expo在向我显示这个sintax错误后停止工作,如果我不能更新某些代码,Expo不会响应,而console向我展示了这个“构建JavaScript包[========================================================================]99%”我必须关闭服务器并再次打开它,才能看到一些变化。嗨,是的,我关闭了下面的类,整个类太大了,我刚刚分享了代码显示的片段,这就是syntaxError。这对我来说很奇怪,因为我对其他类使用了相同的结构,并且总是工作的。代码的整个类都是eq我正在使用Expo,可能错误就在那里,当我用一些简单的更改更新代码时,Expo没有反应,我必须再次打开服务器和Expo应用程序。我不知道“lint工具”我将对此进行搜索。谢谢你能给我看一下你文件中以119行结尾的代码块吗?我刚刚更新了代码。这是整个类。你的导出是什么样子的?抱歉,但你是什么意思?当我尝试运行代码时,这是控制台显示给我的“[22:10:38]SyntaxError:/home/guillermo/ggProject/App.js:意外标记,应为“}”(119:23)[22:10:38][22:10:38]117 |[22:10:38]118 |类Cartas扩展组件{[22:10:38]>119 |构造函数{[22:10:38]| ^[22:10:38]120 |超级(props);[22:10:38]121 |此.state={[22:10:38]122 |卡塔:0,“