I'm trying to hide my API Key for when I commit to github, and I've looked through the forum for guidance, especially the following post:
How do I hide API key in create-react-app?
I made the changes and restarted yarn. I'm not sure what I'm doing wrong––I added an .env
file to the root of my project (I named it process.env
) and in the file I just put REACT_APP_API_KEY = 'my-secret-api-key'
.
I'm thinking it might be the way I'm adding the key to my fetch
in App.js, and I've tried multiple formats, including without using the template literal, but my project will still not compile.
Any help is much appreciated.
performSearch = (query = 'germany') => {
fetch(`https://api.unsplash.com/search/photos?query=${query}&client_id=${REACT_APP_API_KEY}`)
.then(response => response.json())
.then(responseData => {
this.setState({
results: responseData.results,
loading: false
});
})
.catch(error => {
console.log('Error fetching and parsing data', error);
});
}
npm run start
. – n00bAppDevWARNING: Do not store any secrets (such as private API keys) in your React app! Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files.
– Nishant Mehta