【问题标题】:React Functional Component - Use Callback and Props at Same TimeReact 功能组件 - 同时使用回调和道具
【发布时间】:2021-10-03 21:47:09
【问题描述】:

我正在尝试将 React JSX 作为一个非常新的东西来学习。我想同时使用父组件和道具中的回调函数,但我无法处理。我尝试了很多组合,例如 ({childToParent}, props) - ({childToParent, props})

我总是遇到不同的错误。我只想在我的子组件上定义我的“childToParent”回调和道具。有人可以帮我吗?非常感谢!

P.S:列表组件是我的子组件的名字

export default function List(props, {childToParent}) {

....

} 

【问题讨论】:

    标签: reactjs rxjs react-hooks


    【解决方案1】:

    将回调放入 props:

    // Parent calls child:
    <List someProp="foo" childToParent={someCallback} />
    
    // Child:
    const List = ({ someProp, childToParent }) => {
      // use someProp and childToParent here
    };
    

    【讨论】:

      猜你喜欢
      • 2020-05-23
      • 2020-04-30
      • 2021-02-27
      • 2021-10-22
      • 2021-02-28
      • 2020-04-16
      • 2020-08-14
      • 2020-09-15
      • 2017-02-19
      相关资源
      最近更新 更多