简体   繁体   English

JS: func 不是 function (onClick)

[英]JS: func is not a function (onClick)

I have main file with a function in it:我有一个 function 的主文件:

readFromInput = (value) => {
  console.log(value)
}

And I have an additional file with a function in it which allows me to render some button:我还有一个附加文件,其中包含 function,它允许我渲染一些按钮:

export default React.forwardRef(function(props,ref) {
    const {message,severity, flag, func}=props
    ......}

I want to call the onClick method in the last one with use of function from the first one:我想在最后一个中调用 onClick 方法,并使用第一个中的 function :

<Button onClick={() => {func(value)}}>Ok</Button>

How should I call it?我该怎么称呼它?

You can export your function and import it where you need it.您可以导出您的 function 并将其导入您需要的地方。

// main function

export const someFunction() => {
    console.log("functionstuff")
}

// some file 1
import { someFunction } from './components/someFunction'

// some file 2
import { someFunction } from './components/someFunction'

This is what makes React so awesome这就是让 React 如此出色的原因

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

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