簡體   English   中英

React 沒有獲取環境變量

[英]React is not fetching env variables

嘗試在 react 中使用 env 變量,我得到了一個

不明確的

我沒有使用 webpack,只是使用 dotenv。 不知道為什么它不起作用。

我參考了這個

使用 .env 響應 env 變量

它沒有我正在尋找的解決方案。

包.json

{
  "name": "client",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@material-ui/core": "^3.9.1",
    "@material-ui/icons": "^3.0.2",
    "axios": "^0.18.0",
    "history": "^4.7.2",
    "http-proxy-middleware": "^0.19.1",
    "material-ui-icons": "^1.0.0-beta.36",
    "moment": "^2.24.0",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-redux": "^6.0.0",
    "react-router-dom": "^4.3.1",
    "react-scripts": "2.1.3",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "scripts": {
    "start": "PORT=8001 react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ],
  "devDependencies": {
    "dotenv": "^6.2.0"
  }
}

主頁.js

import React, {Component} from 'react';
// import axios from 'axios';
import Grid from '@material-ui/core/Grid';
import Paper from '@material-ui/core/Paper';
import {withStyles} from '@material-ui/core/styles';
import Chip from '@material-ui/core/Chip';
import {signWithGithub} from '../actions/';
import {connect} from 'react-redux';
import {compose} from 'redux';
import Avatar from '@material-ui/core/Avatar';
import Axios from '../Axios'
import {Redirect, withRouter} from 'react-router-dom';
const styles = theme => ({
    root: {
        flexGrow: 1,
        padding: 20
    },
    paper: {
        padding: theme.spacing.unit * 2,
        textAlign: 'center',
        color: theme.palette.text.secondary
    },

    chip: {
        margin: theme.spacing.unit
    }
});

class Home extends Component {

    constructor(props) {
        super(props);

        this.state = {
            user: ""
        }

    }

    componentWillMount = () => {
        console.log(process.env.BASE_GITHUB_SIGNIN);
    }

.env

BASE_URL=http://localhost:8000
BASE_SIGN_IN=http://localhost:8000/api/users/loginUser
BASE_GITHUB_SIGNIN=http://127.0.0.1:8000/api/users/auth/github

在此處輸入圖片說明

如果您使用create-react-app創建了您的應用程序,則 env 具有與此處類似的標准格式。

只需在每個變量前面加上REACT_APP ,就可以了

REACT_APP_YOUR_VARIABLE_NAME

我有同樣的問題。 如果您還沒有完成 .env 文件,請在您的主目錄中完成。 記得啟動你的 React 應用程序,這將允許 React 讀取 .env 文件。 我創建了一個 util 目錄並執行了一個函數調用來返回我的 .env 文件中的變量。 我從我的 get 請求中調用了這些函數。

`export const API_KEY = () => {

  return process.env.REACT_APP_API_KEY; // will return API KEY in .env file.
};

export const API_URL = () => {
  return process.env.REACT_APP_API_URL; // will return API URL in .env file.
};

` 完成此操作后,該函數會將 .env 變量返回給您的 API 調用。

` axios.get(API_URL(), {
      params: { query: term },
      headers: {
        Authorization: `Client-ID ${API_KEY()}`,
      },
    });`

https://trekinbami.medium.com/using-environment-variables-in-react-6b0a99d83cf5

我也遇到了這個問題,我的問題是我將主目錄與安裝 create-react-app 時附帶的 src 目錄混淆了。

第一段幫助了我。 package.json 文件位於主目錄中,.env 文件也應該在該目錄中。

*當 .env 文件有任何更改時,不要忘記重新啟動服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM