Skip to content

atomWithDebounce - Update without debounce when needed #2181

Answered by DavidJFelix
VQuesada asked this question in Q&A
Discussion options

You must be logged in to vote

I made a few modifications -- first, I created a new "action" type modeled after reducer behavior and created a simple action creator that defaults to debounce but if you pass a second argument you can make it immediate. There might be a cleaner way or more preferable way to do this kind of switching.

I also added a new internal atom for the debouncedValue -- having a setter that takes in a complex type and unwraps it cannot use the simplified write only path that was being done before, so what we do is turn our debouncedValue atom into a derived writer that unwraps that action type.

import { atom, SetStateAction } from "jotai";

type DebounceAction<T> = {
  type: "debounce" | "immediate";

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@DavidJFelix
Comment options

Answer selected by VQuesada
@VQuesada
Comment options

@zknicker
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants