-
At some points, we have many states, so there we are using the interface State {
productData: Array<Product>;
isLoading?: boolean;
}
const [state, setState] = useReducer(
(prev: State, next: Partial<State>): State => {
return { ...prev, ...next };
},
{
productData: [],
isLoading: false,
}
); If you want to update the state, you can do it like this: setState({ isLoading: true }); And to get a particular state: state.isLoading; is it possible with |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
You may do this with It does not behave exactly like useReducer, in the sense that it doesn't support a reducer function nor provides a 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
}
}) |
Beta Was this translation helpful? Give feedback.
-
Ohh, that's nice. i'll check that and let you know @franky47. |
Beta Was this translation helpful? Give feedback.
-
@franky47 i guess there is some problem when use
|
Beta Was this translation helpful? Give feedback.
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: