[英]Function passed via a prop to child component is not a function when called
I have a problem when I pass a function from a parent component to a child component via its props and call that function I get an error saying "your function is not a function".当我通过其道具将 function 从父组件传递到子组件并调用 function 时出现问题,我收到一条错误消息,提示“您的 ZC1C425268E68385D1AB5074C17A94F 不是函数”
This is the child component:这是子组件:
const AppointmentItem = (props) => {
const { authRole, data, chosenItem } = props;
...
<Button
onClick={() => chosenItem(card.cardId, 'rejected')} // error is not function
>
...
}
This is the parent component:这是父组件:
const chosenItem = (id, type) => {
setSelectedItem(id);
if (type === 'accepted') operation(id, 'accepted');
if (type === 'rejected') setRejectForm(true);
};
...
<AppointmentItem
// eslint-disable-next-line
key={index}
data={card}
chosenItem={chosenItem}
authRole={authRole}
operation={operation}
/>
When chosenItem
is called in the child component I get an error saying chosenItem
is not a function.在子组件中调用 selectedItem 时,我收到一条错误消息,
chosenItem
不是chosenItem
。
Code looks fine, the problem is either with setSelectedItem
, operation
or setRejectForm
.代码看起来不错,问题出在
setSelectedItem
、 operation
或setRejectForm
。
I suggest you put a debugger;
我建议你放一个
debugger;
statement or a regular breakpoint in chosenItem and inspect each of the functions I mentioned above and see which one is undefined.语句或在 selectedItem 中的常规断点并检查我上面提到的每个函数,看看哪个是未定义的。
If you would like more help/details please upload a reproducible example to code sandbox如果您需要更多帮助/详细信息,请将可重现的示例上传到代码沙箱
you should use proptypes checker to check is function pass as function or not and you should set it to "proptypes.function" and then use proptypes.defaultProps and pass it a null as example --:您应该使用 proptypes 检查器检查 function 是否传递为 function 并且您应该将其设置为“proptypes.function”,然后使用 proptypes.defaultProps 并将其传递给 Z37A6259CC0C1DAE29Z98:8:8897
classname.defaultProps:{myFunction:null}
className.propType:{
myfunction:proptype.function
}
the answer is remove isrequired and use defalutProps答案是 remove isrequired 并使用 defalutProps
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.