Reputation: 1193
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.
Upvotes: 0
Views: 48
Reputation: 185
class JsBasic extends React.Component{
hello = () => {alert('hello')}
render(){
return(
this.hello()
);
}
}
Example click here
Upvotes: 1
Reputation: 2316
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')
);
Upvotes: 3