0

In my non-tyepscript react component I had

componentWillMount() {
    this.delayedSearch = _.debounce((val) => {
        this.onQuerySearch(val);
    }, 1000);
}

for debouncing typing on an input field. Then on an input field I had <input onChange={event => this.delayedSearch(e.value)}>

Now, however, when I switch to Typescipt, my assignment of the _.debounce gives me an error:

Property 'delayedSearch' does not exist on type 'UserSearch'.

How can I fix this?

1 Answer 1

2

That's because your class doesn't declare to have this delayedSearch property, therefor the compiler can't understand what this property is.

In your class you should have something like:

class MyComponent extends React.Component<MyProps, MyState> {
    private delayedSearch: (val: string) => void;

    componentWillMount() {
        // should be fine now
        this.delayedSearch = _.debounce((val) => {
            this.onQuerySearch(val);
        }, 1000);
    }
}
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.