Skip to content
This repository has been archived by the owner on Sep 10, 2022. It is now read-only.

Support for throttleProp and debounceProp #168

Open
wuct opened this issue May 17, 2016 · 9 comments
Open

Support for throttleProp and debounceProp #168

wuct opened this issue May 17, 2016 · 9 comments

Comments

@wuct
Copy link
Contributor

wuct commented May 17, 2016

throttleProp

throttleProp(
  propName: string,
  wait: number 
): HigherOrderComponent

Only update a prop at most once per every wait milliseconds.

debounceProp is similar. Might be useful when a prop is updated frequently. How do you think?

@acdlite
Copy link
Owner

acdlite commented May 17, 2016

Throttling and debouncing are good ideas, but what do you mean by throttling a specific prop? Either the whole component updates or not, right?

@acdlite
Copy link
Owner

acdlite commented May 17, 2016

Oh I think I see what you mean:

  • When the component receives an update, it checks which props have changed using strict equality comparison.
  • If any non-throttled props have changed, those props updates are applied immediately.
  • If the throttled prop has changed, it is applied using a throttled update method.

Is that right?

@wuct
Copy link
Contributor Author

wuct commented May 18, 2016

@acdlite Yes. In my mind it will be something like this

 mapPropsStream(props$ =>
  Observable.combineLatest(
    props$
      .pluck('throttledProps')
      .distinctUntilChanged()
      .throttle(900),
    props$
      .map(({ throttledProps, ...otherProps }) => otherProps)
      .distinctUntilChanged(undefined, shallowEqual),
    (throttledProps, otherProps) => ({ ...otherProps, throttledProps })
  )
)

but without Observable.

@wuct
Copy link
Contributor Author

wuct commented May 18, 2016

I can implement them if they look good

@acdlite
Copy link
Owner

acdlite commented May 19, 2016

Sure!

@wuct wuct mentioned this issue May 20, 2016
@acdlite
Copy link
Owner

acdlite commented May 23, 2016

@wuct Maybe implement these with middleware and submit the PR to my update-middleware branch?

@wuct
Copy link
Contributor Author

wuct commented May 24, 2016

@acdlite sure. I will find some time for it this week :)

@deepsweet
Copy link
Contributor

Hey guys, just implemented these two as a part of my HOCs collection – https://github.com/deepsweet/hocs

@gastonmorixe
Copy link

gastonmorixe commented Jun 17, 2018

With lodash

import throttle from "lodash/throttle" 

const throttleHandler = (
  propName,
  delay,
  options = {
    leading: true,
    trailing: true
  }
) => WrappedComponent =>
  class extends React.PureComponent {
    original = () => {
      this.props[propName]()
    }

    originalThrottled = throttle(this.original, delay, options)

    replace = {
      [propName]: this.originalThrottled
    }

    render() {
      return <WrappedComponent {...this.props} {...this.replace} />
    }
  }

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

No branches or pull requests

4 participants