1

The following code would have worked happily in JavaScript. I am using React+TypeScript, hence this is a JSX file:

Calling component:

<FiltersPanel onFiltersChanged={() => { console.log('I was called') }} />

Inner Component:

const FiltersPanel = (onFiltersChanged) => {

    const handleFiltersChange = () => {
        onFiltersChanged(); /* I am getting `TypeError: onFiltersChanged is not a function` */
    };

    return (<div onClick={handleFiltersChange}/> );
};

export default FiltersPanel;

Why is TypeScript complaining that it cannot find the function, while the function is certainly passed as a prop.

1
  • Is onFiltersChanged null? Commented Aug 27, 2019 at 2:01

1 Answer 1

4

You passed the function as a props so you should receive it to your component as a props like this

const FiltersPanel = (props) => {  }

then use it like this

props.onFiltersChanged()

OR you can use destructuring like this

const FiltersPanel = ({onFiltersChanged}) => { }

so you can use it without the props object like this

onFiltersChanged()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.