Issues when using FastStore hooks
Common issues when using FastStore hooks in your store project, including import errors, context errors, stale data, performance issues, and TypeScript errors.
Keywords: FastStore | Hooks | React | Context errors | Performance | TypeScript
FastStore hooks give you access to built-in store features and state. During implementation, you may encounter issues such as import errors, context errors, stale data, performance regressions, or type mismatches.
Use the table below to identify your issue and follow the corresponding solution.
| Issue | Troubleshooting instructions |
|---|---|
Import error, or the hook returns undefined when used in a component. | Hook not available |
The error Hook must be used within Provider is displayed when calling the hook. | Context errors |
| A hook returns outdated information that doesn't reflect the latest state. | Stale data |
| Hooks cause unnecessary re-renders or noticeably slow down your store. | Performance issues |
| Type mismatches or missing type definitions when using a hook. | TypeScript errors |
Solutions
Hook not available
If you see an import error or the hook returns undefined when used in a component, the corresponding package may not be installed correctly, or the hook may be imported from the wrong source.
To solve this issue, follow these steps:
- Check if the corresponding package is installed in your project (
@faststore/componentsfor UI hooks or@faststore/corefor analytics and experimental hooks). - Confirm that the installed package version is compatible with your FastStore version.
- Make sure your app is wrapped with
UIProviderif you're using UI hooks (such asuseUIoruseSearch). - Make sure you're importing the hook from the correct package.
Context errors
If you see the error Hook must be used within Provider, the hook is being called outside the context provider it depends on.
To solve this issue, follow these steps:
- Wrap the component tree with the required provider, depending on the hook in use:
UIProviderforuseUIanduseSearch.SKUMatrixProviderforuseSKUMatrix.ProductComparisonProviderforuseProductComparison.
- Make sure the provider is placed high enough in the component tree to wrap all components that consume the hook.
- Make sure the provider isn't rendered conditionally, since this can break the context during re-renders.
Stale data
If a hook returns outdated information that doesn't reflect the latest state of your store, the issue is usually related to React dependency arrays, store subscriptions, or cached GraphQL data.
To solve this issue, follow these steps:
- Review the dependencies of any
useEffectcalls that rely on the hook output to ensure they include all relevant values. - Verify that your store subscriptions are correctly set up and not being unsubscribed unexpectedly.
- If you're using GraphQL hooks, clear the cache to force a fresh data fetch.
- Look for issues with
useMemooruseCallbackdependencies that may be preserving stale references.
Performance issues
If hooks are causing unnecessary re-renders or noticeably slowing down your store, the most common causes are missing memoization or unbounded updates from search and filter hooks.
To solve this issue, follow these steps:
- Wrap expensive computations with
useMemoto avoid recalculating them on every render. - Wrap event handlers with
useCallbackto prevent unnecessary re-creation of functions. - Apply debouncing to search and filter hooks to limit the frequency of updates.
- Use
useDelayedFacets_unstableanduseDelayedPagination_unstableon high-traffic product listing pages. - Lazy-load content using visibility hooks (such as
useSlideVisibility) to reduce initial render cost.
TypeScript errors
If you see type mismatches or missing type definitions when using FastStore hooks, the issue is typically related to missing @types packages, incorrect imports, or outdated FastStore versions.
To solve this issue, follow these steps:
- Install the corresponding
@typespackages, if available, for any third-party dependencies used alongside the hook. - Import the required types directly from the FastStore hook package.
- Use type assertions only when necessary, and prefer narrowing the type via runtime checks when possible.
- Confirm that your FastStore version is up to date, since type definitions may evolve between releases.
If the issue continues after following these steps, open a ticket with VTEX Support.