I am trying to learn ReactJS and how Javascript code interacts with the components in ReactJS and am not able to get a simple example to work.
Given below is my index.js
file:
class JsBasic extends React.Component{
render(){
return(
var hello = () => alert('hello')
hello()
);
}
}
ReactDOM.render(
<JsBasic/>
document.getElementById('root')
);
On doing a npm start
, I get the following error:
./src/index.js
Syntax error: Unexpected token (53:6)
51 |
52 | return(
> 53 | var hello = () => alert('hello')
| ^
54 | hello()
55 | );
56 | }
Any help greatly appreciated.
You can't put variables inside return, try this instead:
class JsBasic extends React.Component{
render(){
var hello = () => alert('hello')
return(hello());
}
}
ReactDOM.render(
<JsBasic/>
document.getElementById('root')
);
class JsBasic extends React.Component{
hello = () => {alert('hello')}
render(){
return(
this.hello()
);
}
}
Example click here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.