-
Notifications
You must be signed in to change notification settings - Fork 36
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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?
const on = useCallback( | ||
(event: string, callback: Function, ctx?: any) => | ||
client.current.on(event, callback, ctx), | ||
[] | ||
) as IFlagContextValue['on']; |
There was a problem hiding this comment.
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?
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, []); |
const updateContext = useCallback( | ||
async (context: IMutableContext) => | ||
await client.current.updateContext(context), | ||
[] | ||
) as IFlagContextValue['updateContext']; |
There was a problem hiding this comment.
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']>(
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