'Setting PUBLIC-URL in .env file React application
Hi I am new in react application. I want to set Public_URL in .env file .
.env file (I set my url link)
PUBLIC_URL="http://localhost:8000/api";
Login.tsx file
const response = await fetch("/login", {
method: 'POST',
headers: {'Content-Type': 'application/json'},
credentials: 'include',
body: JSON.stringify({
userName,
password
})
});
When i start the application, My application should be work fine. But In my Login page I submit the Login button My Url call to http://localhost:3000/login. But I need to set http://localhost:8000/api . I don't know how to set it. Please send the correct example.
Package.json
"scripts": {
"start": "env-cmd -f .env react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Solution 1:[1]
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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | cansu |
| Solution 2 | Pramod Kumar |

