0.1.18 • Published 5 years ago

@render-props/throttle v0.1.18

Weekly downloads
879
License
MIT
Repository
github
Last release
5 years ago

Throttle

A state container which limits the frequency with which setState can be called using requestAnimationFrame.

This is a perfect component for capturing the state from events like scroll, keypress and size.

Throttling example image

Installation

yarn add @render-props/throttle or npm i @render-props/throttle


Usage

import Throttle from '@render-props/throttle'


function ThrottledBodyScroller () {
  return (
    <Throttle initialState={{scrollY: 0, gt30: false}}>
      {({throttleState, state}) => (
        <body
          onScroll={
            e => throttleState(
              prevState => (
                window.scrollY > 30
                ? {gt30: true, scrollY: window.scrollY}
                : {gt30: false, scrollY: window.scrollY}
              )
            )
          }
        >
          Greater than 30? {String(state.gt30)}
        </body>
      )}
    </Throttle>
  )
}

Props

  • initialState {object}: the state which this component should initialize with. This component is NOT controlled so you cannot update the state of this component by changing the property value here.

Render Props

Methods

  • throttleState: this function is setState wrapped in a throttle function which uses requestAnimationFrame to limit the number of calls to setState. This is useful for reducing the burden of rapidly-fired events like scrolling.

State

  • state {object}: in addition to the throttleState method, this component provides the state exactly as its been set using throttleState
0.1.18

5 years ago

0.1.17

5 years ago

0.1.16

5 years ago

0.1.15

5 years ago

0.1.14

5 years ago

0.1.13

5 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago