/ src / hooks / useDynamicConfig.ts
useDynamicConfig.ts
 1  import React from 'react'
 2  import { getDynamicConfig_BLOCKS_ON_INIT } from '../services/analytics/growthbook.js'
 3  
 4  /**
 5   * React hook for dynamic config values.
 6   * Returns the default value initially, then updates when the config is fetched.
 7   */
 8  export function useDynamicConfig<T>(configName: string, defaultValue: T): T {
 9    const [configValue, setConfigValue] = React.useState<T>(defaultValue)
10  
11    React.useEffect(() => {
12      if (process.env.NODE_ENV === 'test') {
13        // Prevents a test hang when using this hook in tests
14        return
15      }
16      void getDynamicConfig_BLOCKS_ON_INIT<T>(configName, defaultValue).then(
17        setConfigValue,
18      )
19    }, [configName, defaultValue])
20  
21    return configValue
22  }