Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debounce or Throttle not working with TypeScript/React #16

Closed
martinmckenna opened this issue Jun 20, 2018 · 4 comments
Closed

Debounce or Throttle not working with TypeScript/React #16

martinmckenna opened this issue Jun 20, 2018 · 4 comments

Comments

@martinmckenna
Copy link

martinmckenna commented Jun 20, 2018

I'm having trouble getting Debounce to work correctly. Here's what I have so far (not sure if you've used React, but basically the handleChangeQuery function will run when the <TextField/> component is typed into)

  handleChangeQuery = (e: any) => {
    this.setState({ query: e.target.value }); // this is running properly
    debounce(200, false, () => console.log('hello world')); // not console logging
  }

  render() {
    const { query } = this.state;
    return (
      <React.Fragment>
        <TextField
          InputProps={{
            placeholder: 'search for something',
            value: query,
            onChange: this.handleChangeQuery,
          }}
        />
      </React.Fragment>
    );
  }
}

for some reason the debounce function is not running at all despite this line before it running properly. Is there some step I'm missing?

e: FYI, I tried using the typings provided and then shortly after deleted the typings file and instead went with https://www.npmjs.com/package/@types/throttle-debounce.

@martinmckenna
Copy link
Author

martinmckenna commented Jun 22, 2018

I found a solution. Here's what worked for me

class DebouncedSearch extends React.Component<CombinedProps, State> {

  state: State = {
    query: '',
    debouncedSearch: debounce(400, false, this.props.onSearch)
  };

  handleChangeQuery = (e: any) => {
    const { debouncedSearch } = this.state;
    this.setState({ query: e.target.value });
    debouncedSearch(e.target.value);
  }

  render() {
    const { query } = this.state;
    return (
      <React.Fragment>
        <TextField
          fullWidth
          InputProps={{
            placeholder: 'search for something',
            value: query,
            onChange: this.handleChangeQuery,
          }}
        />
      </React.Fragment>
    );
  }
}

@franklixuefei
Copy link

@niksy
Copy link
Owner

niksy commented Jul 26, 2018

Hi @martinmckenna, I’m currently not using TypeScript so unnfortunately I don’t know how to help you :(

Maybe @franklixuefei’s answer works for you?

@martinmckenna
Copy link
Author

this can be closed. I posted my solution as the first comment @niksy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants