useSearchForm
TheuseSearchForm hook manages search form state, user interactions, and synchronization with URL parameters. It implements debouncing for text input and manages refs for form elements.
Location
/src/hooks/useSearchForm.js
Function Signature
Parameters
The hook accepts a configuration object:| Parameter | Type | Description |
|---|---|---|
onTextFilter | function | Callback when text input changes (debounced) |
onSearch | function | Callback when filters change |
idTechnology | string | Unique ID for technology select |
idLocation | string | Unique ID for location select |
idExperienceLevel | string | Unique ID for experience level select |
idText | string | Unique ID for text input |
Return Value
Returns an object with handlers and refs:| Property | Type | Description |
|---|---|---|
searchText | string | Current search text state |
handleSubmit | function | Form submission handler |
handleTextChange | function | Text input change handler (debounced) |
handleClearFilters | function | Clear all filters handler |
inputRef | RefObject | Ref for text input element |
selectRefTechnology | RefObject | Ref for technology select |
selectRefLocation | RefObject | Ref for location select |
selectRefExperienceLevel | RefObject | Ref for experience level select |
Usage Example
Fromsrc/components/SearchFormSection/SearchFormSection.jsx:
Handler Functions
handleSubmit(event)
Handles form submission and filter changes. Behavior:- Prevents default form submission
- Extracts form data using
FormData - Ignores text input changes (handled separately)
- Calls
onSearchwith filter values
handleTextChange(event)
Handles text input changes with 500ms debouncing. Behavior:- Updates local
searchTextstate immediately - Cancels previous timeout if exists
- Calls
onTextFilterafter 500ms of inactivity
handleClearFilters(event)
Resets all filters and form inputs. Behavior:- Prevents default button behavior
- Calls
onTextFilterwith empty string - Calls
onSearchwith empty filters - Resets all form element values via refs
- Clears local
searchTextstate
URL Synchronization
The hook synchronizes form element values with URL parameters:- On mount: Sets form values from URL
- On URL change: Updates form to match URL
- Enables browser back/forward navigation
Ref Management
The hook creates refs for all form elements:- Direct DOM manipulation for clearing values
- Synchronizing with URL without re-renders
- Accessing form elements imperatively
State Management
Search Text State
Timeout Ref
Best Practices
1. Use Unique IDs
Generate unique IDs with React’suseId:
2. Debounce Text Input
Always debounce search inputs to reduce API calls:3. Separate Text and Filter Handlers
Use different callbacks for text and select filters:4. Clear All State Together
When clearing, reset all related state:Common Patterns
Integration with useFilters
Controlled vs Uncontrolled
This hook uses a hybrid approach:- Uncontrolled: Uses
defaultValueand refs for form elements - Controlled: Uses
searchTextstate for display/logic
Form Change Events
The form usesonChange on the <form> element:
handleSubmit whenever any select changes.
Debouncing Implementation
The debounce pattern:Related Hooks
- useFilters - Provides the callbacks this hook uses
- useRouter - Alternative for simple navigation forms
Dependencies
react:useEffect,useRef,useState,useIdreact-router-dom:useSearchParams