Skip to content

useScrollThreshold

Returns a boolean state that indicates if the user has passed a specified scroll threshold.

Parameters

  1. It can be:
  • a custom scroll event handler that returns a boolean which indicates if the user has passed the threshold or not.
  • or an object that has the scroll offset top, bottom, left, and right values.

INFO

If you specified a custom scroll threshold handler, the offset values will not work.

  1. the target element reference (default is window).

Return Values

NameTypeDescription
refRefObjectthe target element reference.
passedBooleanindicates if the user has passed the threshold.

Example Usage

  1. Using a custom handler:
tsx
import { useScrollThreshold } from 'react-pre-hooks';

export default function Example() {
  const { passed } = useScrollThreshold(() => {
    return window.scrollY > 650;
  });

  return (
    <main>
      <p>Scroll down..</p>
      <div className={passed ? 'show' : ''}>You've reached the end!</div>
    </main>
  );
}
import { useScrollThreshold } from 'react-pre-hooks';

export default function Example() {
  const { passed } = useScrollThreshold(() => {
    return window.scrollY > 650;
  });

  return (
    <main>
      <p>Scroll down..</p>
      <div className={passed ? 'show' : ''}>You've reached the end!</div>
    </main>
  );
}
  1. Using the offset values:
tsx
import { useScrollThreshold } from 'react-pre-hooks';

export default function Example() {
  const { passed } = useScrollThreshold({ bottom: 200 });

  return (
    <main>
      <p>Scroll down..</p>
      <div className={passed ? 'show' : ''}>You've reached the end!</div>
    </main>
  );
}
import { useScrollThreshold } from 'react-pre-hooks';

export default function Example() {
  const { passed } = useScrollThreshold({ bottom: 200 });

  return (
    <main>
      <p>Scroll down..</p>
      <div className={passed ? 'show' : ''}>You've reached the end!</div>
    </main>
  );
}

Released under the MIT License.