react-native-dotenv icon indicating copy to clipboard operation
react-native-dotenv copied to clipboard

Optional import?

Open amcsi opened this issue 6 years ago • 3 comments

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?

amcsi avatar May 30 '18 15:05 amcsi

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!

tomchinery avatar Jul 27 '18 15:07 tomchinery

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.

amcsi avatar Jul 28 '18 09:07 amcsi

Any success on this?

DavitVosk avatar Mar 23 '20 15:03 DavitVosk