Getting Uncaught Exception TypeError: handleFilter is not a function when running the test case in react

24 Views Asked by At

I am passing a parameter from child functional component to parent component. The value gets passed successfully, but when i run the test cases I get the error TypeError: handleFilter is not a function. I've tried everything, is there any solution for the same. I am attaching the sample snippet of it here

Here is the snipshot

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
    }
}
1

There are 1 best solutions below

0
Bharti Sharma On

I think it's because the type is any in child component. You just need to define it as a void function like below:-

handleFilterValue: () => void

Please let me know if it works for you.