0

If I have 3 urls for an api:

http://www.xxxx.dev
http://www.xxxx.test
http://www.xxxx.prod

Is there a way in React or can React recognise the environment for api calls for example when I call:

If I am on dev it calls

fetch('http://www.xxxx.dev')

But when I am on different environments in can call the api url for that environment?

Sole
  • 3,100
  • 14
  • 58
  • 112
  • 1
    Does this answer your question? [Adding an .env file to React Project](https://stackoverflow.com/q/49579028/328193) – David Nov 22 '21 at 12:20
  • As suggested above, use .env variables, and have different variable values depending on your build target. You can have as many variable variations as you need in your build pipeline if you are using Azure DevOps or similar for your builds. – Tor Raswill Nov 22 '21 at 12:24
  • So how would that work, I am using Azure devops, how would I use those variables in the api calls? – Sole Nov 22 '21 at 12:24
  • @Sole: The environment variables would essentially be in `.env` files, configured for different environments. Take a look here: https://create-react-app.dev/docs/adding-custom-environment-variables/ You'd set up the build/deploy processes to use these when producing the resulting deployment. – David Nov 22 '21 at 12:36

0 Answers0