乐闻世界logo
搜索文章和话题

How Do I Use useReducer with useQuery?

1个答案

1

In React applications, useReducer and useQuery are two powerful Hooks used for managing state and data fetching, respectively. useReducer provides a more granular state management approach, allowing us to handle complex state logic through defined actions and a reducer. useQuery, from the React Query library, primarily handles asynchronous data queries, providing features such as data caching, background updates, and other optimizations.

Combining useReducer with useQuery aims to decouple asynchronous data fetching logic from component state management, enabling clearer handling of internal state and external data. Here is a scenario for implementing this combination:

Scenario Description

Suppose we are developing a user management interface requiring fetching a user list from the backend while allowing sorting and filtering on the frontend. Here, fetching the user list can be managed by useQuery, while sorting and filtering state management can be implemented using useReducer.

Implementation Steps

  1. Define the Reducer: We first define a reducer to handle sorting and filtering logic.

    javascript
    const filterReducer = (state, action) => { switch (action.type) { case 'SET_SORT': return { ...state, sort: action.payload }; case 'SET_FILTER': return { ...state, filter: action.payload }; default: return state; } };
  2. Use useReducer: In the component, use useReducer to obtain the sorting and filtering state along with the corresponding dispatch method.

    javascript
    const [state, dispatch] = useReducer(filterReducer, { sort: 'asc', // Default ascending sort filter: '' // Default no filtering });
  3. Use useQuery: Use useQuery to fetch data from the backend, dynamically adjusting the query based on the current sorting and filtering state.

    javascript
    const { isLoading, error, data } = useQuery(['users', state.sort, state.filter], () => fetchUsers(state.sort, state.filter) );
  4. Combine Usage: In the UI, display the user list based on fetched data and state, while providing interactive elements for adjusting sorting and filtering.

    jsx
    return ( <div> <button onClick={() => dispatch({ type: 'SET_SORT', payload: 'asc' })}>Ascending</button> <button onClick={() => dispatch({ type: 'SET_SORT', payload: 'desc' })}>Descending</button> <input value={state.filter} onChange={(e) => dispatch({ type: 'SET_FILTER', payload: e.target.value })} placeholder="Filter users..." /> {error && <div>An error occurred: {error.message}</div>} {isLoading ? ( <div>Loading...</div> ) : ( <ul> {data.map(user => ( <li key={user.id}>{user.name}</li> ))} </ul> )} </div> );

Conclusion

By combining useReducer and useQuery, we can more effectively and clearly manage state and data within React components, making business logic more modular and maintainable. This pattern is particularly suitable for scenarios involving complex state logic and dependencies on remote data.

2024年8月5日 11:21 回复

你的答案