How to use use-interval - 3 common examples

To help you get started, we’ve selected a few use-interval examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github perrin4869 / react-stay-scrolled / examples / src / main.jsx View on Github external
const App = () => {
  const divRef = useRef(null);
  const { stayScrolled } = useStayScrolled(divRef);
  const [messages, setMessages] = useState(initialMessages);

  useInterval(() => {
    setMessages((prevMessages) => prevMessages.concat([message]));
  }, 500);

  useLayoutEffect(() => {
    stayScrolled();
  }, [messages]);

  return (
    <div style="{style}">
      {/* eslint-disable-next-line react/no-array-index-key */}
      {messages.map(({ text }, i) =&gt; <div>{`${text} ${i}`}</div>)}
    </div>
  );
};
github element-motion / element-motion / packages / dev / src / Toggler.tsx View on Github external
children,
}: TogglrProps) =&gt; {
  const [shown, setShown] = React.useState(false);
  const [interacted, setInteracted] = React.useState(false);
  const [inView, setRef] = useIsInViewport({});
  const state = useVisible();
  const set = (value: any) =&gt; {
    setInteracted(true);
    setShown(value);
  };
  const toggle = (value?: any) =&gt; {
    setInteracted(true);
    setShown((prevVal: any) =&gt; (prevVal ? false : value || true));
  };

  useInterval(() =&gt; {
    if (!interval || interacted || !inView || state.hidden) {
      return;
    }

    const result = onIntervalSet(shown);
    setShown((prevVal: any) =&gt; (result !== undefined ? result : !prevVal));
  }, intervalMs);

  return (
    &lt;&gt;
      <div>
      {children({
        toggle,
        set,
        shown,
      })}</div>
github leonardodino / forex-web-app / src / hooks / forex.js View on Github external
const useForexInterval = fn => useInterval(fn, INTERVAL, true)

use-interval

React hook for setting an interval as posted on overreacted.io

MIT
Latest version published 3 years ago

Package Health Score

50 / 100
Full package analysis

Popular use-interval functions