Skip to content

perf: O3-5300 optimize search components with useMemo and useCallback hooks#353

Open
sanks011 wants to merge 5 commits intoopenmrs:mainfrom
sanks011:perf/optimize-search-components
Open

perf: O3-5300 optimize search components with useMemo and useCallback hooks#353
sanks011 wants to merge 5 commits intoopenmrs:mainfrom
sanks011:perf/optimize-search-components

Conversation

@sanks011
Copy link

@sanks011 sanks011 commented Aug 31, 2025

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR implements React performance optimizations across multiple search components in the cohort builder application to reduce unnecessary re-renders and improve memory usage.

Changes Made:

  • Add useMemo for static arrays in SearchByDemographics (genders, livingStatuses)
  • Add useMemo for static arrays in SearchByConcepts (observationOptions, whichObservation)
  • Add useMemo for methods array in SearchByLocation
  • Add useCallback for event handlers across all search components
  • Add .vscode to gitignore for better IDE integration
  • Reduces unnecessary re-renders and improves memory usage
  • Maintains full functionality - all tests passing (26/26 tests)

Performance Benefits:

  • Reduced component re-renders through memoization of static data
  • Better memory usage by preventing recreation of objects/functions
  • Improved user experience during frequent interactions
  • Follows React best practices for component optimization

Components Optimized:

  • SearchByDemographics - Static arrays and event handlers memoized
  • SearchByConcepts - Observation options and functions optimized
  • SearchByLocation - Methods array and handlers optimized
  • SearchByEnrollments - Event handlers optimized

Screenshots

Related Issue

Jira Ticket: https://openmrs.atlassian.net/browse/O3-5300

Other

All existing functionality is preserved. No breaking changes. TypeScript and ESLint compliant. Added .vscode to gitignore for better development experience.

sanks011 and others added 4 commits August 31, 2025 20:00
- Add useMemo for static arrays in SearchByDemographics (genders, livingStatuses)
- Add useMemo for static arrays in SearchByConcepts (observationOptions, whichObservation)
- Add useMemo for methods array in SearchByLocation
- Add useCallback for event handlers across all search components
- Reduces unnecessary re-renders and improves memory usage
- Maintains full functionality - all tests passing
- Add useMemo for static arrays in SearchByDemographics (genders, livingStatuses)
- Add useMemo for static arrays in SearchByConcepts (observationOptions, whichObservation)
- Add useMemo for methods array in SearchByLocation
- Add useCallback for event handlers across all search components
- Add .vscode to gitignore for better IDE integration
- Reduces unnecessary re-renders and improves memory usage
- Maintains full functionality - all tests passing
@sanks011 sanks011 changed the title perf: optimize search components with useMemo and useCallback hooks perf: O3-5300 optimize search components with useMemo and useCallback hooks Dec 29, 2025
@anjula-sack anjula-sack requested a review from Copilot January 16, 2026 09:34
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements React performance optimizations by adding useMemo and useCallback hooks to prevent unnecessary re-renders in search components of the cohort builder application.

Changes:

  • Add memoization hooks to static configuration arrays in search components
  • Add useCallback to event handlers to prevent function recreation on every render
  • Add .vscode directory to .gitignore for better IDE integration support

Reviewed changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
.gitignore Adds .vscode directory to ignored files and updates comment to be more generic
search-by-location.component.tsx Adds useMemo for methods array and useCallback for handleResetInputs and submit functions
search-by-enrollments.component.tsx Adds useCallback for handleResetInputs and submit functions
search-by-demographics.component.tsx Adds useMemo for genders and livingStatuses arrays, and useCallback for reset and submit functions
search-by-concepts.component.tsx Adds useMemo for observationOptions and whichObservation arrays, and useCallback for handleReset, getOnOrBefore, and handleSubmit functions

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

await onSubmit(composeJson(params), queryDescriptionBuilder(observations, concept.name));
setIsLoading(false);
};
}, [concept, operator, operatorValue, getOnOrBefore, onOrBefore, onOrAfter, timeModifier, onSubmit]);
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dependency array includes getOnOrBefore which is itself a memoized function. This creates an unnecessary dependency chain since getOnOrBefore already depends on lastDays and lastMonths. Instead of including getOnOrBefore in the dependencies, include its underlying dependencies lastDays and lastMonths directly, or call getOnOrBefore() inline without making it a separate memoized function.

Suggested change
}, [concept, operator, operatorValue, getOnOrBefore, onOrBefore, onOrAfter, timeModifier, onSubmit]);
}, [concept, operator, operatorValue, lastDays, lastMonths, onOrBefore, onOrAfter, timeModifier, onSubmit]);

Copilot uses AI. Check for mistakes.
], [t]);

const reset = () => {
const reset = useCallback(() => {
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function name reset is inconsistent with the naming pattern used in other search components. Other components use handleResetInputs or handleReset for similar functionality. Consider renaming to handleReset for consistency.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants