Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Optional import? #41

Open
amcsi opened this issue May 30, 2018 · 3 comments
Open

Optional import? #41

amcsi opened this issue May 30, 2018 · 3 comments

Comments

@amcsi
Copy link

amcsi commented May 30, 2018

I'm importing MOCK_CURRENT_LOCATION_LATITUDE from react-native-dotenv, however I'm not expecting it to always be set, and thus in the code I check with an if to see if it's set.

Instead I get Try to import dotenv variable "MOCK_CURRENT_LOCATION_LATITUDE" which is not defined in any .env files. and a redbox error for it even though I intentionally do not want it set in my env.

What am I supposed to do to have optional environment variables?

@tomchinery
Copy link

tomchinery commented Jul 27, 2018

You could set your env variable to a empty string and check its length to determine if it's been set or not. For example:

In .env:

MOCK_CURRENT_LOCATION=

In your code somewhere:

import { MOCK_CURRENT_LOCATION } from 'react-native-dotenv';

export default SomeClass extends React.Component {
  render() {
     if (MOCK_CURRENT_LOCATION.length > 0) {
       return (<SomeView></SomeView>);
     }
     return (null);
  }
}

Hope that helps!

@amcsi
Copy link
Author

amcsi commented Jul 28, 2018

Thanks, but it would be nicer to not have to define all the optional environment variables like in other dotenv libraries for other programming languages.

Phil-Barber pushed a commit to Phil-Barber/react-native-dotenv that referenced this issue Dec 30, 2019
@DavitVosk
Copy link

Any success on this?

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

No branches or pull requests

3 participants