简体   繁体   English

React 组件'解析错误:意外的令牌,预期的“;”'渲染后

[英]React Component 'Parsing error: Unexpected token, expected “;”' after render

I am trying to render some complex tertiary logic that organizes my application's components in the correct order.我正在尝试呈现一些复杂的三级逻辑,以正确的顺序组织我的应用程序的组件。 It keeps giving me an error at the bracket immediately after the render method.它在渲染方法之后立即在括号处给我一个错误。

I've found the same error described for similar situations on this website, but the solutions didn't help mine.我在本网站上发现了针对类似情况描述的相同错误,但解决方案对我没有帮助。 Most of the troubleshooting I read about involved fixing how/where people called methods but this script is only running render.我读到的大多数故障排除都涉及修复人们调用方法的方式/位置,但此脚本仅运行渲染。 Additionally I made sure to wrap it in one tag and use className instead of class and all other JSX related syntax bug fixes.此外,我确保将其包装在一个标签中并使用 className 而不是 class 和所有其他与 JSX 相关的语法错误修复。

Here is my App.js: I am passing along functions into the other components that toggle their respective states of the same name between true and false determining which gets rendered.这是我的 App.js:我将函数传递给其他组件,这些组件在 true 和 false 之间切换它们各自的同名状态,以确定哪个被渲染。

import React, { Component } from 'react';
import './index.css';
import Game from './Game.js'
import Homescreen from './Homescreen.js'
import PlayerScreen from './Playerscreen.js'

class App extends Component {
  constructor(props){
    super(props);
    this.state = {
      Homescreen: true,
      PlayerScreen: false,
      Game: false,
      players: [{
        users: '',
        points: 0,
    }]
  }

  render() {
    return (
      <div>
      {
        (this.state.Homescreen) ?
        <div id="wrapper">
          <Homescreen newScreen={()=>this.setState({Homescreen: false, PlayerScreen: true})}></Homescreen>
        </div> : (this.state.PlayerScreen) ? <div id="wrapper">
          <PlayerScreen players={this.state.players} start={(players)=>this.setState({PlayerScreen: false, Game: true, players: players})}></PlayerScreen>
        </div> : <div id="wrapper"><Game players={this.state.players}></Game></div>
      }
      </div>
      );
    }
  }
}

export default App;

Error message:错误信息:

Failed to compile.

./src/App.js
  Line 20:12:  Parsing error: Unexpected token, expected ";"

  18 |   }
  19 | 
> 20 |   render() {
     |            ^
  21 |     return (
  22 |       <div>
  23 |       {

Any help would be greatly appreciated.任何帮助将不胜感激。

You are missing a curly brackets in your state declaration.您的 state 声明中缺少大括号。

constructor(props){
    super(props);
    this.state = {
      Homescreen: true,
      PlayerScreen: false,
      Game: false,
      players: [{
        users: '',
        points: 0,
    }]
  } // this is missing
}

the message is expecting a semi colon there because you haven't closed your constructor method;)该消息需要一个分号,因为您尚未关闭构造函数方法;)

Take a break, walk around your apt/house/office give your eyes a second to catch these little things休息一下,在你的公寓/房子/办公室里走一走,让你的眼睛稍等片刻,捕捉这些小东西

      import React, { Component } from 'react';
      import './index.css';
      import Game from './Game.js'
      import Homescreen from './Homescreen.js'
      import PlayerScreen from './Playerscreen.js'

      class App extends Component {
        constructor(props) {
          super(props);
          this.state = {
            Homescreen: true,
            PlayerScreen: false,
            Game: false,
            players: [{
              users: '',
              points: 0,
            }]
          }
        }
        render() {
          return (
            <div>
              {
                (this.state.Homescreen) ?
                  <div id="wrapper">
                    <Homescreen newScreen={() => this.setState({ Homescreen: false, PlayerScreen: true })} />
                  </div> :
                  (this.state.PlayerScreen) ?
                    <div id="wrapper">
                      <PlayerScreen players={this.state.players} start={(players) => this.setState({ PlayerScreen: false, Game: true, players: players })} />
                    </div> :
                    <div id="wrapper">
                      <Game players={this.state.players} />
                    </div>
              }
            </div>
          );
        }
      }
      export default App

replace this code with yours and check用您的代码替换此代码并检查

Make these changes to your main file.对您的主文件进行这些更改。

showComponent = (component) => {
      this.setState({currentComponent: component})
    }


let checkCurrentScreen = this.state.currentScreen;
              {checkCurrentScreen === "homeScreen" ? (
                 <Homescreen showComponent={this.state.showComponent} />
                  ) : checkCurrentScreen ? (
                    <PlayerScreen showComponent={this.state.showComponent} />
                    ) : <DefaultScreen />
               }

Pass in your component with button click通过单击按钮传入您的组件

const handleComponentChange = (e, component) => {
    e.preventDefault();
    props.showComponent(component)
  }

I apologize for my type o, I have corrected above.我为我的 o 型道歉,我已经在上面更正了。 The last method is what I use in a separate component, I like this approach be cause it allows t his method to be used almost anywhere.最后一种方法是我在单独的组件中使用的方法,我喜欢这种方法,因为它几乎可以在任何地方使用他的方法。 Here is an example to the button used.这是使用的按钮的示例。

<button 
      onClick={e => handleComponentChange(e, "author")}
      className="comment-submit-button">Cancel
      </button>

This button will pass "e" and the name of the component to the method above the return.这个按钮会将“e”和组件的名称传递给返回上面的方法。 This allows for this method to be portable, here is the full method, perhaps this will help.这允许这种方法是可移植的,这是完整的方法,也许这会有所帮助。

const handleComponentChange = (e, component) => { // passed in e
    e.preventDefault(); // for this
    props.showComponent(component);  // called method from index.js
  }

With my handleComponentChange I can pass in any props/state to use as I wish to use, plus the name of my component I want to render.使用我的handleComponentChange,我可以传入任何我希望使用的道具/状态,以及我想要渲染的组件的名称。 I was able to easily add e for prevent default, plus the name.我可以很容易地添加 e 以防止默认,加上名称。

ShowComponent sits in my index file, where I have my components that will be conditionally rendered. ShowComponent 位于我的索引文件中,其中我的组件将被有条件地呈现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM