简体   繁体   English

简单的ReactJS Javascript输入不起作用

[英]Simple ReactJS Javascript input not working

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. 我正在尝试学习ReactJS,以及Javascript代码如何与ReactJS中的组件交互,并且无法获得一个简单的示例来工作。

Given below is my index.js file: 下面给出的是我的index.js文件:

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: 在执行npm start ,出现以下错误:

./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: 您不能将变量放入return中,请尝试以下操作:

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 示例点击这里

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

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