Hi, I have a question about React Native and `subs...
# ask-questions
e
Hi, I have a question about React Native and
subscribeToChanges
. • In the example it isn't used: https://github.com/growthbook/examples/blob/main/react-native-cli/App.tsx • And when I set it up https://docs.growthbook.io/lib/react#streaming-updates it doesn't appear to work (updating the toggle in GrowthBook doesn't update the app) • I am using a vanilla RN app running on the iOS simulator Does
subscribeToChanges
work for React Native?
f
it should support server send events right?
are you able to see if it has the connection open?
you can try adding the subscribeToChanges: true
in that example, you can add it after line 35
e
Copy code
const gb = new GrowthBook({
  apiHost: '<redacted>',
  clientKey: '<redacted>',
  // Enable easier debugging during development
  enableDevMode: true,
  // Update the instance in realtime as features change in GrowthBook
  subscribeToChanges: true,
  backgroundSync: true,
  // Only required for A/B testing
  // Called every time a user is put into an experiment
  trackingCallback: (experiment, result) => {
    console.log('Experiment Viewed', {
      experimentId: experiment.key,
      variationId: result.key,
    });
  },
});
This is what I have - it loads the value in the application but it does not appear to open a connection to subscribe. Is there a debug log I can look at?
f
What do you have on the SDK side in GrowthBook?
e
Copy code
import {
  GrowthBook,
  GrowthBookProvider,
  useFeatureIsOn,
} from '@growthbook/growthbook-react';
I also load in the feature definitions https://github.com/growthbook/examples/blob/main/react-native-cli/App.tsx#L57
This is the
App.tsx
file below.
const enabled = useFeatureIsOn('show-the-button');
doesn't load
isEnabled
works only after loading the JSON URL where it's set Neither update automatically when I toggle on the growthbook website.
Copy code
/**
 * Sample React Native App
 * <https://github.com/facebook/react-native>
 *
 * @format
 */

import React, {useEffect, useState} from 'react';
import type {PropsWithChildren} from 'react';
import {
  GrowthBook,
  GrowthBookProvider,
  useFeatureIsOn,
} from '@growthbook/growthbook-react';
import {
  SafeAreaView,
  ScrollView,
  StatusBar,
  StyleSheet,
  Text,
  useColorScheme,
  View,
} from 'react-native';

const gb = new GrowthBook({
  apiHost: '<redacted>',
  clientKey: '<redacted>',
  // Enable easier debugging during development
  enableDevMode: true,
  // Update the instance in realtime as features change in GrowthBook
  subscribeToChanges: true,
  backgroundSync: true,
  // Only required for A/B testing
  // Called every time a user is put into an experiment
  trackingCallback: (experiment, result) => {
    console.log('Experiment Viewed', {
      experimentId: experiment.key,
      variationId: result.key,
    });
  },
});

import {
  Colors
} from 'react-native/Libraries/NewAppScreen';

type SectionProps = PropsWithChildren<{
  title: string;
}>;

function Section({children, title}: SectionProps): JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';

  return (
    <View style={styles.sectionContainer}>
      <Text
        style={[
          styles.sectionTitle,
          {
            color: isDarkMode ? Colors.white : Colors.black,
          },
        ]}>
        {title}
      </Text>
      <Text
        style={[
          styles.sectionDescription,
          {
            color: isDarkMode ? Colors.light : Colors.dark,
          },
        ]}>
        {children}
      </Text>
    </View>
  );
}

function App(): JSX.Element {
  const isDarkMode = useColorScheme() === 'dark';
  const [isReady, setIsReady] = useState(false);
  const [isEnabled, setIsEnabled] = useState(false);

  useEffect(() => {
    setIsEnabled(gb.getFeatureValue('show-the-button', false));
  }, [isReady, setIsEnabled]);

  const enabled = useFeatureIsOn('show-the-button');

  const backgroundStyle = {
    backgroundColor: isDarkMode ? Colors.darker : Colors.lighter,
  };

  useEffect(() => {
    // Load feature definitions from GrowthBook API
    fetch(
      '<feature definition JSON URL>',
    )
      .then(res => res.json())
      .then(json => {
        gb.setFeatures(json.features);
      })
      .catch(e => console.error('Failed to fetch features', e))
      .finally(() => {
        setIsReady(true);
      });
  }, []);
  return (
    <GrowthBookProvider growthbook={gb}>
      <SafeAreaView style={backgroundStyle}>
        <StatusBar
          barStyle={isDarkMode ? 'light-content' : 'dark-content'}
          backgroundColor={backgroundStyle.backgroundColor}
        />

        <ScrollView
          contentInsetAdjustmentBehavior="automatic"
          style={backgroundStyle}>
          <View
            style={{
              backgroundColor: isDarkMode ? Colors.black : Colors.white,
            }}>
            <Section title="show-the-button with useFeatureIsOn">
              <Text>{enabled ? 'Yes' : 'No'}</Text>
            </Section>
            <Section title="show-the-button with getFeatureValue">
              <Text>{isEnabled ? 'Yes' : 'No'}</Text>
            </Section>
          </View>
        </ScrollView>
      </SafeAreaView>
    </GrowthBookProvider>
  );
}

const styles = StyleSheet.create({
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
  },
  highlight: {
    fontWeight: '700',
  },
});

export default App;
ping ^
f
@happy-autumn-40938 do you know?
h
My guess is that you'd need to plug in an EventSource polyfill to handle the subscriptions in React Native. I'm not hugely familiar with the RN ecosystem, but we have an example of how you might do this using Node, which I suspect will be very similar to how you'd do it here: https://docs.growthbook.io/lib/js#nodejs