简体   繁体   English

反应:错误:意外的令牌

[英]React:error:unexpected token

I am getting an error in this code in render() function on the line where I do return where it says unexpected token.Here is the code.我在 render() 函数中的这段代码中出现错误,在我返回的地方显示了意外令牌。这是代码。

class  Game extends React.Component {
   constructor(props){
      super(props);
      this.size=3;
      this.board=this.initilaizefun(this.size);
      this.state={
         rows:this.size,
         columns:this.size,
         arr:this.board
      }
   }
    shuffle =(array)=>{

    }
   //now we fill values from o to 8 in board
   initilaizefun=(size)=>{

  }

   rendergrid=()=>{

 }

   render() {
     return (
     <div className="game">
      <h1>PUZZLE</h1>
     </div>
     );
   }
}
ReactDOM.render(<Game />, document.getElementById('root'));

You renderGrid function contains multiple syntactic errors.您的 renderGrid 函数包含多个语法错误。 Also while writing React code, you need to configure babel with webpack or standalone cdn同样在编写 React 代码时,您需要使用 webpack 或独立 cdn 配置 babel

rendergrid function will be like rendergrid函数会像

rendergrid = () => {
    let arr1 = Array(this.state.size);
    console.log(arr1);
    return arr1.map((val, index1) => {
      return <div className="board">
        {
          arr1.map((val, index2) => {
            let val2 = this.state.arr[index1][index2].value;
            return <button className="button" >{val2}</button>;
          })
        }
      </div>
    })
  }

working codesandbox工作代码沙盒

If you don't wish to use webpack, you can use cdn like如果你不想使用 webpack,你可以使用 cdn 之类的

<div id="root"></div>

<script src="build/react.min.js"></script>
<script src="build/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.25.0/babel.min.js"></script>
<script src="app.jsx" type="text/babel"></script>

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

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