zetachang / react-native-dotenv

A Babel preset let you import application configs from .env file (zero runtime dependency)
MIT License
915 stars 68 forks source link

Optional import? #41

Open amcsi opened 6 years ago

amcsi commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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.

DavitVosk commented 4 years ago

Any success on this?