在react中运行测试用例时出现未捕获的异常类型错误:handleFilter不是函数

问题描述 投票:0回答:1

我正在将参数从子功能组件传递到父组件。该值已成功传递,但是当我运行测试用例时,我收到错误

TypeError: handleFilter is not a function
。我已经尝试了所有方法,是否有相同的解决方案。我在这里附上它的示例片段

这是截图

const ParentComponent = () => {
    const getFilterValue = (filter:string) => {
        setFilter(filter);
    };

return (
     <ChildComponent handleFilterValue={getFilterValue} />
)
}

///////////

const ChildComponenet = ({handleFilterValue}: any) => {  
    const applyFIlters = () => {
        //some logic here <----- the filter is passing from the logic here
         handleFilterValue(filter) <--------- On this line I am getting error while running testcase
    }
}
reactjs callback react-functional-component vitest
1个回答
0
投票

我认为这是因为子组件中的类型是any。您只需将其定义为 void 函数,如下所示:-

handleFilterValue: () => void

请告诉我它是否适合您。

© www.soinside.com 2019 - 2024. All rights reserved.