RTK Query中useLazyQuery钩子的错误处理及状态参数获取方法
useLazyQuery Error Handling & State Access Hey there! Let's tackle your two questions about RTK Query's useLazyQuery—I've run into similar quirks before, so I know exactly what you're dealing with.
1. How to Handle Errors with useLazyQuery
First off, the error value from useLazyQuery does work, but there are a couple of common reasons you might not be seeing it:
- You're trying to access it too early (before the query completes or fails)
- You're not accounting for how RTK Query surfaces errors (either via the returned state or the trigger's promise result)
Here are two reliable ways to handle errors:
Option 1: Use the Returned error and isError State
Your existing destructuring already grabs prodError, but you should pair it with the isError flag to ensure you only access the error when it's actually present. Update your destructuring to include isError, then render or log the error conditionally:
const [trigger, { loading: prodLoading, error: prodError, data: prodData, isError // Add this flag } ] = prodApi.endpoints.getAllProducts.useLazyQuery(); // In your component JSX if (isError) { return <div className="error-message">Oops! {prodError.message}</div>; }
Option 2: Catch Errors Directly from the trigger Promise
The trigger function returns a promise that resolves to an object containing either data or error. You can wrap your trigger call in logic to check for errors (note: RTK Query usually surfaces business errors in the result.error field rather than throwing them—catch is mainly for network-level failures):
const fetchProducts = async (queryParams) => { const result = await trigger(queryParams); if (result.error) { // Handle business/logic errors here console.error("Query failed:", result.error); alert(`Failed to load products: ${result.error.message}`); } else { // Use result.data if needed (though you can also use prodData from the hook) console.log("Products loaded:", result.data); } };
2. Accessing isLoading, isSuccess, and Other States Like useQuery
Good news: useLazyQuery exposes all the same state properties as useQuery—you just need to destructure them! The loading field you're already using is actually an alias for isLoading, but you can explicitly pull in all the states you're used to:
const [trigger, { data: prodData, error: prodError, loading: prodLoading, // Same as isLoading isLoading, isSuccess, isError, isFetching, refetch // You even get refetch if needed } ] = prodApi.endpoints.getAllProducts.useLazyQuery();
These states work exactly like they do with useQuery:
isLoading: True when the query is first running (and no previous data exists)isSuccess: True when the query completed successfullyisError: True when the query failedisFetching: True whenever the query is in flight (including refetches)
Why You Might Not Have Seen These Before
RTK Query's TypeScript definitions (if you're using TS) will show all available states, but even in plain JS, you can destructure any of these properties directly from the hook's returned object.
内容的提问来源于stack exchange,提问作者DhruvK

