我正在做reactjs的第一步。這段代碼應該寫 「ON」,但我得到的錯誤:如何在React Component中定義函數?
App.js: Unexpected token, expected (
代碼:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
class Light extends React.Component {
constructor(props) {
super(props);
this.state = {light:"On"};
};
function formatLightState() {
return <h1>{this.state.light}</h1> ;
}
render() {
return (
<div>
{this.formatLightState()}
</div>
);
}
}
class App extends React.Component {
constructor(props) {
super(props);
}
renderLight(){
return <Light />
}
render() {
return (
<div>
{this.renderLight()}
</div>
);
}
}
export default App;
我缺少什麼?
感謝!它的工作。我無法相信我爲此浪費了這麼多時間:( – user3879322