简体   繁体   English

从父组件向下传递 function 到子组件不起作用

[英]Pass down function from parent component to child component is not working

The function is not triggering. function 未触发。 I checked many other answers and it seems like my code should be working.我检查了许多其他答案,看来我的代码应该可以工作。

In App parent component:在 App 父组件中:

update() {
  console.log("hello");
}

<PrivateRoute
  exact
  path="/profile"
  component={Profile}
  update={this.update}
/>

In Profile child component:在 Profile 子组件中:

<button
  type="button"
  onClick={this.props.update}
>

I think in PrivateRoute you are not passing update method to Profile component.我认为在PrivateRoute您没有将update方法传递给Profile组件。

const PrivateRoute = ({ component: Component, path: Path, ...rest }) => (
//...rest of your code

         //pass update prop along with other props which are needed.
          <Component update={rest.update} /> 

//...rest of your code
      );
    }}
  />
);

Hope this helps.希望这可以帮助。

暂无
暂无

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

相关问题 如何从2级子级组件将参数传递给父级 - How to pass arguments to parent from a child component down at 2 levels 将状态从子组件传递到父组件 - Pass state from child component to parent component 如何将值从子组件(基于函数的组件)传递到父组件(基于类的组件) - How to pass value from child component(function based component )to parent component(class based component) 如何在 React 中将事件从子组件传递到父组件返回到另一个子组件 - how to pass an event from a child component to parent component back down to another child in React 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 通过 React 功能组件将 function 从父级传递给子级 - Passing down function from parent to child through React functional component 从子组件调用父组件 function - Call a parent component function from a child component 从父组件执行子(组件)功能 - Executing child (component) function from the parent component 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 如果子组件为动态组件,如何将数据从子组件传递给父组件 - How to pass data from Child to Parent, if Child component dynamic component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM