8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【React】.envで定義した環境変数がundefinedになった場合の対応方法

Last updated at Posted at 2022-02-05

概要

Reactの学習の一環で外部APIを利用するためのAPIキー.envで管理することにした

いざ、process.env.環境変数のキー名で読み込むとundefinedが戻ってきた場合の対応方法をまとめる

参考資料

環境変数の定義

  • REACT_APP_xxxx としないと認識してくれないので注意
  • .envは package.jsonと同階層に作成すること
.env
REACT_APP_DEMO_API_KEY=xxxxhoge

環境変数の呼び出し

App.tsx
`/hoge?api_key=${process.env.DEMO_API_KEY}`

サーバーの再起動

yarn start

ここまでの手順を踏めば環境変数の値を取得することができます

さいごに

いいねしていただけると記事執筆の励みになりますので、参考になったと思った方は是非よろしくお願いします!

8
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?