带有InitialState和componentDidMount的React代码将不会运行

React code with InitialState and componentDidMount will not run

本文关键字:运行 代码 React InitialState componentDidMount 带有      更新时间:2023-09-26

我得到的错误是:

SyntaxError:
/home/lucy/Desktop/Lucy/Comms/src/components/imgGallery.js
Unexpected token (12:2) while parsing file:
/home/lucy/Desktop/Lucy/Comms/src/components/imgGallery.js

但无论我做什么或删除什么,代码都不会运行,错误所在的指示都会改变,但这都是

我的代码:

import React from 'react'
import ReactDOM from 'react-dom'
ImgGallery = React.createClass ({
  getInitialState(){
    return {
      images: [{"image: "a"}]
    }
  }
  componenDidMount(){
   }
  render (){
   return  (
  <div>
    <div className="imgGallery">
    </div>
   </div>
  )
  }
})

在文档中,当您使用createClass时,您有一个pojo。因此,必须使用javascript文字表示法声明函数:

React.createClass({
  getInitialState: function() {return ....},
  componenDidMount: function(){},
  render: function() {
   return  (
      <div>
      <div className="imgGallery">
      </div>
     </div>;)
  }
});