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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions src/FlagProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @format */

import React, { type FC, type PropsWithChildren, useEffect, useMemo, useState } from 'react';
import { type IConfig, UnleashClient } from 'unleash-proxy-client';
import React, { type FC, type PropsWithChildren, useCallback, useEffect, useMemo, useState } from 'react';
import { type IConfig, IMutableContext, UnleashClient } from 'unleash-proxy-client';
import FlagContext, { type IFlagContextValue } from './FlagContext';

export interface IFlagProvider {
Expand Down Expand Up @@ -106,20 +106,47 @@ const FlagProvider: FC<PropsWithChildren<IFlagProvider>> = ({
};
}, []);

const on = useCallback(
(event: string, callback: Function, ctx?: any) =>
client.current.on(event, callback, ctx),
[]
) as IFlagContextValue['on'];
Comment on lines +109 to +113
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, []);


const off = useCallback(
(event: string, callback?: Function) => client.current.off(event, callback),
[]
) as IFlagContextValue['off'];

const isEnabled = useCallback(
(toggleName: string) => client.current.isEnabled(toggleName),
[]
) as IFlagContextValue['isEnabled'];

const updateContext = useCallback(
async (context: IMutableContext) =>
await client.current.updateContext(context),
[]
) as IFlagContextValue['updateContext'];
Comment on lines +125 to +129
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']>(


const getVariant = useCallback(
(toggleName: string) => client.current.getVariant(toggleName),
[]
) as IFlagContextValue['getVariant'];

const context = useMemo<IFlagContextValue>(
() => ({
on: ((event, callback, ctx) => client.current.on(event, callback, ctx)) as IFlagContextValue['on'],
off: ((event, callback) => client.current.off(event, callback)) as IFlagContextValue['off'],
updateContext: async (context) => await client.current.updateContext(context),
isEnabled: (toggleName) => client.current.isEnabled(toggleName),
getVariant: (toggleName) => client.current.getVariant(toggleName),
on,
off,
updateContext,
isEnabled,
getVariant,
client: client.current,
flagsReady,
flagsError,
setFlagsReady,
setFlagsError,
}),
[flagsReady, flagsError]
[flagsReady, flagsError, on, off, updateContext, isEnabled, getVariant]
);

return (
Expand Down
Loading