Skip to content

Difference between Lodash/debounce() and Zustand transient updates? #696

Closed Answered by lkdm
lkdm asked this question in Q&A
Discussion options

You must be logged in to vote

Answer

You can use Zustand with debounce.

Is this the best way?

import { useCallback, useEffect, useRef } from "react";
import create from "zustand";
import _ from 'lodash';

const useStore = create(set => ({
  value: "test"
}))


function TextInput(this: any) {

  // Set the default value, without enforcing its state.
  const handleRef = useRef<HTMLInputElement | null>(null)
  useEffect(() => {
    if (handleRef.current)
      handleRef.current.value = "test"
  }, [handleRef]);

  const debounce = useCallback(
    // Delay saving state until user activity stops
    _.debounce((_inputString: string) => {
      useStore.setState({value: _inputString})
      // API Calls go here
      const…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by lkdm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant