Overview
TheuseFilter hook provides access to the global filter context in the CryptoTracker application. It allows components to read and update filter criteria for searching and filtering cryptocurrencies by text, minimum price, and maximum price.
This hook must be used within a
FilterProvider component. If used outside the provider, it will throw an error.Signature
Return Value
The hook returns an object with the following properties:The current filter state object containing:
text(string): Search text to filter by cryptocurrency name or symbolminPrice(number | null): Minimum price filter in USDmaxPrice(number | null): Maximum price filter in USD
Function to update the filter state. Can accept either a new
Filter object or a function that receives the previous state and returns a new state.Filter Interface
TheFilter type is defined as:
Search string to match against cryptocurrency names and symbols. Case-insensitive matching is recommended.
Minimum price threshold in USD. Set to
null to disable minimum price filtering.Maximum price threshold in USD. Set to
null to disable maximum price filtering.Usage Example
Here’s how the hook is used in theHomeScreen component to filter cryptocurrency data:
Context Provider
Before using this hook, wrap your application with theFilterProvider:
App.tsx
Error Handling
If the hook is used outside of aFilterProvider, it will throw an error:
FilterProvider component.