Description:
Qif is a React component that provides a robust, performant filtering system. It simplifies filter management by centralizing filter state and synchronizing it with URL search parameters.
Developers can easily integrate customizable, composable filter components into their applications, enhancing user experience and code maintainability.
Features
- Centralized State Management: Manage all your filter states in one place.
- Headless UI: Style filter components to match your application’s design seamlessly.
- Composable Components: Create complex filter UIs by combining smaller, reusable components.
- URL Synchronization: Automatically sync filter states with URL search parameters for easy sharing and bookmarking.
- Developer-Friendly API: A clear and concise API simplifies implementation and reduces development time.
- Performance Optimized: Efficiently handles filtering, even with extensive datasets.
Use Cases
- E-commerce Product Filtering: Allow users to refine product searches by categories, price ranges, brands, and other attributes. Qif simplifies the implementation of complex filtering logic and maintains a clean URL structure for each filtered view.
- Data Table Filtering: Enhance data tables with interactive filters, enabling users to quickly isolate relevant information. Qif’s centralized state management ensures consistent filtering across multiple columns and data types.
- Dashboard Customization: Empower users to personalize dashboards by filtering displayed data based on specific criteria, such as date ranges, metrics, or user roles.
- Content Management Systems: Integrate Qif to enable content filtering based on tags, authors, publication dates, and other metadata, simplifying content discovery for both administrators and end-users.
- Real Estate Listings: Facilitate property searches by filtering listings based on location, price, property type, and features. Qif’s URL synchronization allows users to share specific filtered searches.
Installation
# Using npm
npm install react-qif
# Using yarn
yarn add react-qifUsage
1. Setting up the FiltersProvider:
The core of Qif’s functionality lies within the FiltersProvider component. This component acts as a container, managing the filter state and providing access to its associated functions. First, import necessary modules:
import { FiltersProvider, useFilters } from 'react-qif';
import { useState } from 'react';Next, wrap the part of your application that requires filtering with the FiltersProvider. Initialize your filter state using useState:
const [filters, setFilters] = useState({});
<FiltersProvider syncSearchParams filters={filters} setFilters={setFilters}>
{/* Your filter components and content to be filtered */}
</FiltersProvider>The filters state variable will hold the current filter values, while setFilters is the function used to update these values. The syncSearchParams prop, when set to true, automatically synchronizes filter state with the URL’s search parameters.
2. Creating Custom Filter Components:
Qif encourages a composable approach to building filters. Create individual filter components for each filterable attribute. Inside these components, use the useFilters hook to interact with the filter state provided by the FiltersProvider:
const FilterComponent = ({ name, defaultValue = '' }) => {
const { register, setValue, getValue } = useFilters();
useEffect(() => {
register(name, defaultValue);
}, [name, register, defaultValue]);
const handleChange = (e) => setValue(name, e.target.value);
return (
<input
type='text'
onChange={handleChange}
placeholder={`Filter: ${name}`}
value={getValue(name) || defaultValue}
/>
);
};Let’s break down this example:
useFilters()hook: This hook provides access to several key functions for managing individual filters.register(name, defaultValue): Registers a new filter with the specifiednameand sets its initialdefaultValue. This function should be called within auseEffecthook to ensure it runs only once when the component mounts.setValue(name, value): Updates the value of the filter identified bynamewith the newvalue. This function is typically called within anonChangehandler of an input element.getValue(name): Retrieves the current value of the filter identified byname. This is used to populate the input field with the current filter value.
3. Implementing the onBeforeStateChange Prop (Optional):
For more advanced control over filter state updates, use the onBeforeStateChange prop. This prop accepts a callback function that is invoked before any changes are applied to the filter state. This allows you to intercept and modify the new filter values before they are updated:
const handleBeforeStateChange = (newFilters, reason) => {
// Modify newFilters based on the reason for the change
return newFilters;
};
<FiltersProvider
syncSearchParams
filters={filters}
setFilters={setFilters}
onBeforeStateChange={handleBeforeStateChange}
>
{/* ... */}
</FiltersProvider>The reason parameter indicates the cause of the state change (e.g., a specific filter being updated or all filters being reset).
4. Utilizing the ResetButton Component:
Qif provides a convenient ResetButton component to reset all filters to their default values:
import { ResetButton } from 'react-qif';
// ... inside your component within the FiltersProvider
<ResetButton />This button automatically handles the reset logic using the reset function from the useFilters hook. It also includes a built-in disabled state, managed through the isResetDisabled property, which activates when all filters are at their default values.
FAQs
Q: Does Qif support server-side rendering?
A: Yes, Qif works with SSR frameworks like Next.js and maintains filter states during hydration.
Q: Can I use Qif with TypeScript?
A: Yes, Qif includes TypeScript definitions out of the box.
Q: Will Qif affect my application’s performance?
A: No, Qif uses optimized state updates to minimize re-renders.



