r/reactjs • u/Mob_Pilled • Oct 19 '26
Needs Help React not saving string to local storage
Hi everyone! I just finished a React and course and decided to build my first react app. Currently, I am trying to save a string to browser local storage but it does not persist on refresh. Through some console.logs I was able to see that it in fact DOES save the string to the location specified in. setItem initially but when I refresh the key or value in local storage stops existing.
Here is my code
// function for search button
async function handleClick(){
localStorage.setItem("lastLocationSearched", searchValue);
setData(null);
setIsPending(true);
setError(null);
try{
const value = await fetch(`https://geo.ipify.org/api/v2/country?apiKey=***********&ipAddress=${searchValue}`).then((response)=>{
if (!response.ok){
throw Error("Could not fetch the data for that resource");
}
return response.json();
});
setData(value);
console.log(localStorage.getItem("lastLocationSearched"));
} catch(error){
setError(error.message);
}
setIsPending(false);
};
// useEffect hook
useEffect(()=>{
if ((localStorage.getItem("lastLocationSearched")===null)|| (localStorage.getItem("lastLocationSearched")==="")){
console.log(localStorage.getItem("lastLocationSearched"));
setSearchValue("");
handleClick();
}else{
setSearchValue(localStorage.getItem("lastLocationSearched"));
handleClick();
}
}, []);
Through some digging online I saw someone say that remove the strict mode tags from index.js would fix this problem but it did not for me. Can anyone please assist me?
10
u/Objective_Fly_6750 Oct 19 '26
It is so refreshing to see someone building and debugging like in the good old days.
8
u/SchartHaakon Oct 19 '26
No need for the useEffect, is there?
And if the component runs on the server as well:
4
u/azangru Oct 19 '26
Try slowly reading through your code line by line and asking yourself, what each of the lines does. Right now, there is so much chaos in your snippet that it suggests you were copying and pasting code without properly understanding what it does.
One of the the things that haven't been commented on is these two lines in useEffect:
You haven't shown us what
setSearchValuedoes; but if it is auseStatesetter, as I suspect it is, then `handleClick` will not get the value that you expect in `searchValue`, because that value will only become available during the subsequent render. This would explain why `localStorage.setItem("lastLocationSearched", searchValue)` saves nothing into the local storage.5
u/PrimaryFamous6139 NextJS Pages Router Oct 19 '26
The issue might be that handleClick() runs inside useEffect on every mount and calls localStorage.setItem() with an empty or outdated searchValue, overwriting your saved value. Try separating the initial read from the search logic. Read the value from localStorage in useEffect and only update it when the user submits a search. Also, check whether another part of your app is clearing localStorage. React Strict Mode isn’t normally the cause of localStorage losing data on refresh
1
u/poc-detektd Oct 21 '26
Good call masking the key in the post, but heads up that once this is deployed the key ships to every visitor anyway: it’s right there in the network tab and the JS bundle, so anyone can grab it and burn through your ipify quota. Totally fine while you’re learning locally, but before you put it online, move that fetch behind a tiny backend or serverless function that holds the key and just forwards the IP.
Also worth wrapping the input with
encodeURIComponent(searchValue)(or building the URL withURLSearchParams), otherwise whatever someone types goes straight into the query string and can add extra params to the request.