【发布时间】:2019-12-05 11:01:57
【问题描述】:
我正在使用类组件内部和外部的函数语法。任何人都可以向我解释为什么打印功能在这样写时可以工作
const UploadButton = (props)=> {
const fileName = 'myfile';
props.getFileName(fileName)
function print(){console.log('onClick worked')}
return(
<div>
<input onClick= {print()} type="file" id = {fileName}/>
</div>
)
}
但是当我像在类组件中声明它一样编写它时:
print(){console.log('onClick worked')}
我收到这个错误
Line 10: Parsing error: Unexpected token, expected ";"
8 | props.getFileName(fileName)
9 |
> 10 | print(){console.log('onClick worked')}
| ^
【问题讨论】:
-
错误是什么?还可以尝试留白以使其更具可读性。
-
@JackBashford 我已将其添加到答案中
-
你正在尝试调用它 - 请显示封闭类
标签: javascript function class components constants