[英]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
.因此我无法在
ChildComponent
的props
中访问它。
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 限制了渲染的数量以防止无限循环。
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.