Skip to content

Conversation

@Duncan-Brain
Copy link
Collaborator

@Duncan-Brain Duncan-Brain commented Jan 13, 2026

Description

The branch tests/useref_for_survey is an exploration of react hooks This PR is the result of that.

We want to:

  • avoid rebuilding the Model class when possible

useMemo is appropriate for cases where there are expensive computations. A class instantiation could be considered an expensive computation. But it is not necessary to achieve a single instance instantiation.

useRef is appropriate for cases where we want a really stable reference to something that we do not expect to need to re-render based on that reference.

The example shown in tech-daily for useRef was for the case where surveyJson is not expected to change within the component lifecycle. So changing that prop would have no effect. Something the useMemo version had that the useRef example did not.

Notes

  • I wanted to preserve the useMemo functionality of allowing surveyJson to change
  • I finally found terminology for the useRef pattern:  “Singleton”. Use cases exist but it may not be best for this case, and there can be drawbacks
  • the example shown in the useRef docs the class instance has no props. So it is more valid in that case.
  • re-evaluated the need for a "strong" stable reference => our goal is to reduce not necessarily to prevent re-render
  • the code still does not work properly when useMemo is removed (my main concern)
  • lazy state initialization of useState + useEffect can mimic the helpful properties we are looking for from useMemo and useRef
  • weird thing: the test branch shows very low re-render time for recreating survey: makes me think we profiled the rendering wrong (useMemo recreation = ~40ms, all other methods using useEffect = ~2x ~0.01ms)

Jira link:

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have ordered translation keys alphabetically (optional: run pnpm i18n to help with this)
  • I have added the GNU General Public License to all new files

@Duncan-Brain Duncan-Brain requested review from a team as code owners January 13, 2026 17:45
@Duncan-Brain Duncan-Brain requested review from kathyavini and removed request for a team January 13, 2026 17:45
@Duncan-Brain Duncan-Brain marked this pull request as draft January 13, 2026 17:45
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.

2 participants