Skip to content

Is there way to use like useReducer ? #462

Answered by franky47
Kavan72 asked this question in Q&A
Discussion options

You must be logged in to vote

You may do this with useQueryStates (plural), which supports multiple related search params.

It does not behave exactly like useReducer, in the sense that it doesn't support a reducer function nor provides a dispatch method, but it allows obtaining the previous state and return partial updates:

const [{ productData, isLoading }, setState] = useQueryStates({
  productData: parseAsJson<Product[]>().withDefault([]),
  isLoading: parseAsBoolean.withDefault(false)
})

setState((previousState) => {
  // Return a partial update of the state type to update the relevant values
  // Example:
  return {
    isLoading: !previousState.isLoading
    // This won't touch the productData
  }
})

Replies: 3 comments 11 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by Kavan72
Comment options

You must be logged in to vote
5 replies
@Kavan72
Comment options

@franky47
Comment options

@Kavan72
Comment options

@franky47
Comment options

@Kavan72
Comment options

Comment options

You must be logged in to vote
6 replies
@Kavan72
Comment options

@franky47
Comment options

@Kavan72
Comment options

@franky47
Comment options

@Kavan72
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
2 participants