score:2

Accepted answer

you can set in .env file another variable for your endpoint, for example:

react_app_api_endpoint=http://localhost:8000/api

public_url is already used as base url to react get the dependencies of your application.

in your component you can get the environment variable this way:

const { react_app_api_endpoint } = process.env;

const response = await fetch(`${react_app_api_endpoint}/login`, {
  method: "post",
  headers: { "content-type": "application/json" },
  credentials: "include",
  body: json.stringify({
    username,
    password,
  }),
});

score:1

public_url reference documentation

https://create-react-app.dev/docs/using-the-public-folder/

it must be used for external web resource like icon, image, css. it is meant for internal reference to current web page link.

score:2

you can use custom environment variables for that purpose. also, notice that your custom environment variables should start react_app_ prefix. this is required to access variables in the application.

example : enter image description here


Related Query

More Query from same tag