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

Config fields lazy loaded with new architecture #819

Open
flover0815 opened this issue Nov 18, 2024 · 0 comments
Open

Config fields lazy loaded with new architecture #819

flover0815 opened this issue Nov 18, 2024 · 0 comments

Comments

@flover0815
Copy link

I just upgraded from RN 0.75 to RN 0.76 (new architecture enabled; rn-config v1.5.3) and now have the issue that the Config object appears to be empty. When i log it, it will look like this, with no fields i've defined in my .env file:
{"getConstants": [Function getConstants], "toString": [Function toString]}

Apparently the fields now only get resolved and stored after you access them once directly in your code.
So when i write e.g. Config.API_URL, it would return the URL and if i would now log the Config object, it will include the URL.

Now that means if you access the fields directly through the Config object, it might not be an issue. However in my case i've added a wrapper so that i can parse the Environment fields before i access them, so i can have proper types and auto completion. File looks like this:

import dotenvParseVariables from 'dotenv-parse-variables';
import { Config } from 'react-native-config';

type Env = {
  ENV: string;
  API_URL: string;
  FEATURE_ENABLED: boolean;
};

export const ENV = dotenvParseVariables(Config as { [key: string]: string }) as Env;

Now if i would adjust the code like this, where i first access the fields directly through the Config then they would stay available and i could parse them like before.

export const getEnv = (): Env => {
  console.log(Config.ENV);
  console.log(Config.API_URL);
  console.log(Config.FEATURE_ENABLED);
  return dotenvParseVariables(Config as { [key: string]: string }) as Env;
};

export const ENV = getEnv();

Not sure if this is the intended behaviour

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant