将参数传递给父组件中的功能

时间:2020-04-29 04:55:21

标签: javascript reactjs

我想将数据从子组件传递到父组件

const Main () => {
    const helloUser(name) => console.log("hello, name)
    return <User helloUser={() => helloUser } />
}

const User (props) => {
    const { helloUser } = props
    helloUser("john")
    return <p>User component</p>
}

它不起作用。只有当我在Main组件中传递这样的数据时,它才起作用

return <User helloUser={() => helloUser("john") } />

我做错了什么?谢谢!

1 个答案:

答案 0 :(得分:0)

我错误地传递了()=>

return <User helloUser={helloUser } />