Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context value reference changing across rendering #196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Criezc
Copy link

@Criezc Criezc commented Mar 19, 2025

Related Issues

About the changes

Implemented useCallback to maintain stable function references across render cycles. This prevents performance issues caused when unmemoized functions create new instances on each render.

Important files

  • FlagProvider.tsx:109-150

Copy link
Member

@Tymek Tymek left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution! Would you like to improve types/verbosity before we merge it?

Comment on lines +109 to +113
const on = useCallback(
(event: string, callback: Function, ctx?: any) =>
client.current.on(event, callback, ctx),
[]
) as IFlagContextValue['on'];
Copy link
Member

Choose a reason for hiding this comment

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

Maybe just wrapping a function works here?

Suggested change
const on = useCallback(
(event: string, callback: Function, ctx?: any) =>
client.current.on(event, callback, ctx),
[]
) as IFlagContextValue['on'];
const on = useCallback<IFlagContextValue['on']>(client.current.on, []);

Comment on lines +125 to +129
const updateContext = useCallback(
async (context: IMutableContext) =>
await client.current.updateContext(context),
[]
) as IFlagContextValue['updateContext'];
Copy link
Member

Choose a reason for hiding this comment

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

Even if this cannot be bound directly, useCallback is a generic:

const updateContext = useCallback<IFlagContextValue['updateContext']>(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Approved PRs
Development

Successfully merging this pull request may close these issues.

2 participants