This message was deleted.
# sdk-react
w
This message was deleted.
f
It might be a race condition, are you sure that the useFeatureIsOn is happening after the useEffect that sets the attributes?
b
Thanks Graham, the attribute was not being set in time as you say. To make it work I also followed this: https://maxrozen.com/fetching-data-react-with-useeffect and modified App() ended up being as follows. I hope this saves someone some time. This way GB is seemingly ready to go in other components.
Copy code
export default function App() {
  const nonce = useNonce();
  const data = useLoaderData();
  const [gbLoaded, setData] = useState(null);

  useEffect(() => {
    // Load features from the GrowthBook API and initialize the SDK
    const fetchData = async () => {
      await gb.loadFeatures();
      const getClientIdPromise = new Promise((resolve) => {
        ReactGA.gtag('get', 'G-XXX', 'client_id', resolve);
      })
      await getClientIdPromise.then((clientId) => {
        gb.setAttributes({
          clientId,
        });
      })
      setData(gb)
    }
    fetchData().catch(console.error);
  }, []);

  return (
    <html lang="en">
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1" />
        <Meta />
        <Links />
      </head>
      <body>
        {gbLoaded &&
          <GrowthBookProvider growthbook={gbLoaded}>
            <Layout {...data}>
            < Outlet />
            </Layout>
          </GrowthBookProvider>
        }
        <ScrollRestoration nonce={nonce} />
        <Scripts nonce={nonce} />
        <LiveReload nonce={nonce} />
      </body>
    </html>
  );
}