bable.config.js File plugins: [ ["module:react-native-dotenv", { "envName": "APP_ENV", "moduleName": "@env", "path": ".env", "blocklist": null, "allowlist": null, "blacklist": null, // DEPRECATED "whitelist": null, // DEPRECATED "safe": false, "allowUndefined": true, "verbose": false }] ].env Código de archivo
API_LOGIN=https://example.in/logininiciar sesión.js
import { API_LOGIN } from '@env'; const url = API_LOGIN; let email = this.state.gmail; let password = this.state.password; fetch(url, { method: 'POST', body: JSON.stringify({ email: email, password: password, }), headers: new Headers({ 'Content-Type': 'application/json', }), }) .then((res) => res.json()) .catch((error) => console.error('Error : ', error)) .then((response) => { if (response.access_token != undefined) { this.setState({ accessToken: response.access_token, }); if (this.state.accessToken != '') { this.props.navigation.navigate('VolunteerHome', { AccessToken: this.state.accessToken, }); } } else { alert('Unauthorised User'); } }); En el código anterior, instalé dotenv . Luego agregué el código del complemento en el archivo bable.config.js . luego creé el archivo .env en el directorio raíz y creé la variable ambiental API_Login=example.in/login , luego importé el paquete en el archivo login.js y usé esa variable. Pero obtuve un error de red en mi código si simplemente agrego la URL de la API en el archivo de inicio de sesión y luego mi código se ejecuta correctamente. Por favor ayúdame a resolver este problema
Si intentas usarlo como una cadena?
const url = `${API_LOGIN}`; También puede importarlo así: process.env.API_LOGIN
También necesita require('dotenv').config()
Más información aquí