简体   繁体   English

当父组件和子组件也是功能组件时,如何将 function 作为道具传递?

[英]How to pass a function as props when both Parent and Child component also a functional component?

I have this ParentComponent which I want to pass a function named toggleDrawer to the ChildComponent like this:我有这个ParentComponent ,我想将一个名为toggleDrawer的 function 传递给ChildComponent ,如下所示:

const ParentComponent = () {
     
   const [drawerState, setDrawerState] = useState(false);

   const toggleDrawer = (anchor, open) => {
        setDrawerState(open);
   }

  return(
       <div>
         <IconButton color="primary"                         
                     onClick={toggleDrawer("right", true)}> // here I called to toggleDrawer so the ChildComponent can be shown 
             <SomeIcon />
         </IconButton>
         
        <ChildComponent 
             anchor="right"
             open={drawerState}
             handleDrawerState={toggleDrawer}/> 

        </div>
  )
}

So I get the toggleDrawer function in ChildComponent like this:所以我在 ChildComponent 中得到了toggleDrawer ChildComponent ,如下所示:

const CartDrawer = (props) => {

 // other stuff at the top 

 return(
    <Drawer
      anchor={props.anchor}
      open={props.open}
      onClose={props.handleDrawerState(props.anchor, false)}
    >
  )
}

As you can see I get the handleDrawerState in ChildComponent by accessing it props .如您所见,我通过访问它的props获得了handleDrawerState中的ChildComponent But what I get is:但我得到的是:

TypeError: props.handleDrawerState is not a function类型错误:props.handleDrawerState 不是 function

I tried below, also get the same result:我在下面尝试过,也得到了相同的结果:

const {handleDrawerState} = props

 <Drawer
       ... other stuff 
   onClose={handleDrawerState(props.anchor, false)}
>

So I check the console in browser by console.log(props) , instead having a key with handleDrawerState , I having a object in the props ,which present like this:所以我通过console.log(props)检查浏览器中的控制台,而不是使用带有handleDrawerState的键,我在props中有一个 object ,如下所示:

proto : Object原型:Object

For now, I not understand what I doing wrong, cause as I see here, toggleDrawer in ParentComponent is a function, but passed to ChildComponent it become and object.现在,我不明白我做错了什么,因为正如我在这里看到的, ParentComponent中的toggleDrawer是 function,但传递给ChildComponent它变成了 object。 Therefore I unable to access it in props in ChildComponent .因此我无法在ChildComponentprops中访问它。

Question:问题:

Therefore, what is the correct way to pass a function to ChildComponent ?因此,将 function 传递给ChildComponent的正确方法是什么?

Updated:更新:

If I do like this:如果我这样做:

<Drawer
    ... some other stuff 
    onClose={() => props.handleDrawerState(props.anchor, false)}
>

I get the error like this:我收到这样的错误:

Uncaught Error: Too many re-renders.未捕获的错误:重新渲染过多。 React limits the number of renders to prevent an infinite loop. React 限制了渲染的数量以防止无限循环。

They need to be wrapped in an anon function他们需要被包裹在一个匿名 function

You cannot call a function as a prop if you fire the function when adding it as a prop (unless you want the result of the fired function passed as a prop).如果您在将 function 添加为道具时触发 function 作为道具,则不能将其称为道具(除非您希望将触发的 ZC1C425268E68385D1AB5074C17A94F14 的结果作为道具传递)。

Should be this应该是这个

const ParentComponent = () {
     
   const [drawerState, setDrawerState] = useState(false);

   const toggleDrawer = (anchor, open) => {
        setDrawerState(open);
   }

  return(
       <div>
         <IconButton color="primary"                         
                     onClick={() => toggleDrawer("right", true)}> //anon func here
             <SomeIcon />
         </IconButton>
         
        <CartDrawer 
             anchor="right"
             open={drawerState}
             handleDrawerState={toggleDrawer}/> 

        </div>
  )
}

const CartDrawer = (props) => {

 // other stuff at the top 

 return(
    <Drawer
      anchor={props.anchor}
      open={props.open}
      onClose={() => props.handleDrawerState(props.anchor, false)} // annon func here
    />
  )
}

The way you have it right now it will fire only once when the component mounts.您现在拥有它的方式,它只会在组件安装时触发一次。

<MyComponent 
  onClick={handleClick()} // this will always fire on mount, never do this unless you want the result from the function as a prop and not the function as itself
/>

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

相关问题 如何将值从子功能组件传递给父类组件? - How to pass value from a child functional component to parent class component? 如何将对象作为道具传递给 React.js 中的子功能组件? - How to pass an object as props to a child Functional Component in React.js? 如何将传递函数作为道具发送给子组件 - How to send pass function as props to child component 当父组件改变状态时,子组件不会获得新的道具(功能组件) - Child component not get new props when parent component change state ( Functional Component ) 反应:将 2 个道具从子组件传回同一 function 中的父组件 - React: Pass 2 props back to the parent component in the same function from the child 如何将函数作为道具从一个功能组件传递到另一个? - How to pass function as props from one functional component to another? 如何在功能组件中将默认道具作为 function 传递 - How can I pass default props as function in functional component 如何将props(状态和函数)传递给子路由器组件 - How to pass props (both states and functions) to the child router component 在 React 中将功能组件从子组件传递给父组件 - Pass functional component from child to parent in React 如何将道具从子组件传递到父组件到 ReactJS 中的另一个子组件? - How to pass props from child component to parent component to another child component in ReactJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM