How to use the use-debounce.useDebouncedCallback function in use-debounce

To help you get started, we’ve selected a few use-debounce 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 christiandavid / gatsby-theme-byfolio / @christiandavid / gatsby-theme-byfolio / src / components / hooks / useWnResize.js View on Github external
function useWnResize(ref = false, delay = 500) {
  const [windowSize, setWindowSize] = useState(null)

  const getSize = useCallback(() => {
    // Setting state to the updated matches
    setWindowSize({
      width: ref === false ? window.innerWidth : ref.current.offsetWidth,
      height: ref === false ? window.innerHeight : ref.current.offsetHeight,
    })
  }, [ref])

  const [resizeHandler] = useDebouncedCallback(() => {
    getSize()
  }, delay)

  useEffect(() => {
    if (!isSSR) {
      // Add listener
      window.addEventListener("resize", resizeHandler)
      getSize()
    }

    return () => {
      // Remove listener
      !isSSR && window.removeEventListener("resize", resizeHandler)
    }
  }, [resizeHandler, ref, getSize])
github getredash / redash / client / app / lib / hooks / useSearchResults.js View on Github external
export default function useSearchResults(fetch, { initialResults = null, debounceTimeout = 200 } = {}) {
  const [result, setResult] = useState(initialResults);
  const [isLoading, setIsLoading] = useState(false);
  const currentSearchTerm = useRef(null);
  const isDestroyed = useRef(false);

  const [doSearch] = useDebouncedCallback(searchTerm => {
    setIsLoading(true);
    currentSearchTerm.current = searchTerm;
    fetch(searchTerm)
      .catch(() => null)
      .then(data => {
        if (searchTerm === currentSearchTerm.current && !isDestroyed.current) {
          setResult(data);
          setIsLoading(false);
        }
      });
  }, debounceTimeout);

  useEffect(
    () =>
      // ignore all requests after component destruction
      () => {
github christiandavid / gatsby-theme-byfolio / @christiandavid / gatsby-theme-byfolio / src / components / hooks / useMQResize.js View on Github external
function useMQResize(queries) {
  const [queryMatch, setQueryMatch] = useState(null)
  const keys = useRef(Object.keys(queries))

  const handleQuery = useCallback(() => {
    const updatedMatches = keys.current.reduce((acum, media) => {
      acum[media] = !!window.matchMedia(queries[media]).matches
      return acum
    }, {})

    // Setting state to the updated matches
    setQueryMatch(updatedMatches)
  }, [queries])

  const [resizeHandler] = useDebouncedCallback(
    () => {
      handleQuery()
    },
    // Delay
    500
  )

  useEffect(() => {
    if (!isSSR && window.matchMedia) {
      // Add listener
      window.addEventListener("resize", resizeHandler)
      handleQuery()
    }

    return () => {
      // Remove listener
github sillsdev / appbuilder-portal / source / SIL.AppBuilder.Portal.Frontend / src / ui / components / inputs / auto-saving-input / index.tsx View on Github external
export default function AutoSavingInput({ value, onChange, InputElement, timeout }: IProps) {
  const Input = InputElement || Form.TextArea;
  const [localValue, setLocalValue] = useState(value);
  const save = useDebouncedCallback((newValue) => onChange(newValue), timeout, []);

  const onInputChange = (e: React.KeyboardEvent) => {
    const newValue = e.currentTarget.value;

    if (localValue === newValue) {
      return;
    }

    setLocalValue(newValue);
    save(newValue);
  };

  const onFormSubmit = (e: React.FormEvent) => {
    e.preventDefault();

    onChange(localValue);
github deltachat / deltachat-desktop / src / renderer / components / helpers / ChatList.js View on Github external
if (chatId === 0) return
    if (isChatIdInView(chatId)) {
      log.debug(`useLazyChatListItems: chat with id ${chatId} changed, it's in view therefore refetching`)
      const chats = await fetchChats([chatId], true)
      setChatItems(chatItems => { return { ...chatItems, ...chats } })
    } else {
      log.debug(`useLazyChatListItems: chat with id ${chatId} changed, it's NOT in view, unsetting if needed`)
      setChatItems(chatItems => {
        if (typeof chatItems[chatId] !== 'undefined') return { ...chatItems, [chatId]: undefined }
        return chatItems
      }
      )
    }
  }

  const [onChatListScroll] = useDebouncedCallback(() => {
    fetchChatsInView(20)
  }, 50)

  const onChatListItemChanged = (event, { chatId }) => {
    if (chatId === 0) {
      updateChatsInViewUnsetOthers()
    } else {
      refetchChatIfInViewUnsetOtherwise(chatId)
    }
  }

  const onResize = () => fetchChatsInView()

  useLayoutEffect(() => {
    window.addEventListener('resize', onResize)
    return () => window.removeEventListener('resize', onResize)
github getredash / redash / client / app / visualizations / funnel / Editor / AppearanceSettings.jsx View on Github external
export default function AppearanceSettings({ options, onOptionsChange }) {
  const [onOptionsChangeDebounced] = useDebouncedCallback(onOptionsChange, 200);

  return (
    
      <section>
        <input label="{(<React.Fragment">Number Values Format)}
          className="w-100"
          data-test="Funnel.NumberFormat"
          defaultValue={options.numberFormat}
          onChange={event =&gt; onOptionsChangeDebounced({ numberFormat: event.target.value })}
        /&gt;
      </section>

      <section>
        </section>
github getredash / redash / client / app / visualizations / choropleth / Editor / ColorsSettings.jsx View on Github external
export default function ColorsSettings({ options, onOptionsChange }) {
  const [onOptionsChangeDebounced] = useDebouncedCallback(onOptionsChange, 200);

  return (
    
      <section>
        <select data-test="Choropleth.Editor.ClusteringMode" label="Clustering mode"> onOptionsChange({ clusteringMode })}
        &gt;
          quantile
          equidistant
          k-means
        </select></section>
github getredash / redash / client / app / pages / queries / components / SchemaBrowser.jsx View on Github external
export default function SchemaBrowser({ schema, onRefresh, onItemSelect, ...props }) {
  const [filterString, setFilterString] = useState("");
  const filteredSchema = useMemo(() => applyFilter(schema, filterString), [schema, filterString]);
  const [expandedFlags, setExpandedFlags] = useState({});
  const [handleFilterChange] = useDebouncedCallback(setFilterString, 500);
  const [listRef, setListRef] = useState(null);

  useEffect(() => {
    setExpandedFlags({});
  }, [schema]);

  useEffect(() => {
    if (listRef) {
      listRef.recomputeRowHeights();
    }
  }, [listRef, filteredSchema, expandedFlags]);

  if (schema.length === 0) {
    return null;
  }

use-debounce

Debounce hook for react

MIT
Latest version published 2 months ago

Package Health Score

85 / 100
Full package analysis