Skip to content

useFeatureFlag doesn't re-evaluate on default user change #85

@jacksonrayhamilton

Description

@jacksonrayhamilton

Describe the bug

This seems similar to: #27

The docs for the React SDK recommend setting the default user using client.setDefaultUser: https://configcat.com/docs/sdk-reference/react/#default-user

const currentUser = { identifier: 'john@example.com' };

export const SetConfigCatUserComponent = () => {
  const client = useConfigCatClient();

  const [user] = useState(currentUser);

  useEffect(() => {
    client.setDefaultUser(user);
  }, [client, user]);

  return null;
};

However, it appears that when client.setDefaultUser is called, this doesn't automatically trigger useFeatureFlag() calls throughout a React app to start recalculating their values (in case they might be different based on the new user).

To reproduce

  1. Have useFeatureFlag() calls in your app that will return true if your user is X, false if not
  2. Have the default user be undefined
  3. Asynchronously set the default user to be user X using client.setDefaultUser
  4. Observe that the useFeatureFlag() calls don't start returning true simply as a result of calling client.setDefaultUser

Expected behavior

client.setDefaultUser(), or another SDK-provided function, would integrate with React state and immediately cause useFeatureFlag() calls throughout the app to start returning proper values for the new user.

SDK version

  • configcat-react: 5.0.3
  • @configcat/sdk: 1.0.2

SDK configuration

import { ConfigCatProvider, useConfigCatClient } from "configcat-react";

/** Keeps the ConfigCat client's default user in sync with session */
const ConfigCatUserSync = () => {
    const client = useConfigCatClient();
    const { session } = useSessionContext();

    useEffect(() => {
        if (session) {
            client.setDefaultUser({
                identifier: String(session.idPayor),
                email: session.email ?? undefined,
                custom: { account: String(session.idAccount) }
            });
        } else {
            client.setDefaultUser({ identifier: `anonymous` });
        }
    }, [client, session]);

    return null;
};

const Navigation = () => (
    <SessionProvider>
        <ConfigCatProvider sdkKey={window.settings.CONFIGCAT_SDK_KEY}>
            <ConfigCatUserSync />
            <Router>
                <App />
            </Router>
        </ConfigCatProvider>
    </SessionProvider>
);

Logs

N/A

Language/Framework version

React

Platform

macOS 15, Chrome 145

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions