【问题标题】:Pass function with arguments from parent to child in react在反应中将带有参数的函数从父级传递给子级
【发布时间】:2021-05-21 16:29:55
【问题描述】:

[React 功能组件] 在父组件中,一个按钮 onClick 将触发一个函数,这个触发的函数将向子组件发送另一个带有一些数据的函数。

实际上,我正在尝试实施这种方法,但不能!!!

怎么做?

【问题讨论】:

  • 显示您尝试过的内容,以便我们可以帮助您正确修复它。
  • 请尝试将Minimal, Complete, and Reproducible Code Example 包含在您尝试过的内容中,并包含有关按预期工作或未按预期工作的任何详细信息。

标签: reactjs


【解决方案1】:

有点不清楚您到底想要实现什么,但我会先展示一些您的代码示例。 以下是可能对您有所帮助的 React 文档 https://reactjs.org/docs/handling-events.html

【讨论】:

    【解决方案2】:
    Check if below code works for you...
    --------------------------------------  
      const ChildComponent = ({prop1, prop2}) => {
          return (
            <div>
              <h1>Property one: {prop1}</h1>
              <h1>Property two: {prop2}</h1>
            </div>
          )
        }
        const ParentComponent = ({}) => {
          
          const name = "My Name"
          const age = 27
          return (
              <ChildComponent prop1={name} prop2={age}/>
          )
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-04
      • 1970-01-01
      • 2021-04-08
      • 2022-08-09
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多