In React 18.3.1 when I try to use useState
to set the new ip
with setIp
it doesn’t update.
import { useEffect, useState } from "react";
import { useIsLoading } from "./useIsLoading";
import { useError } from "./useError";
export const useFetchIp = () => {
const [ip, setIp] = useState(null);
const { changeIsLoading } = useIsLoading()
const { changeError } = useError()
useEffect(() => {
changeIsLoading(true)
const FetchIp = async () => {
const url = 'https://api.ipify.org/?format=json';
try {
const response = await fetch(url);
if (response.ok) {
const ipDetail = await response.json()
const ipMain = ipDetail.ip;
setIp(ipMain)
changeError(null)
changeIsLoading(false)
} else {
throw new Error("Couldn't Get The IP")
}
} catch(err) {
changeIsLoading(false)
changeError(err)
}
}
FetchIp()
}, [])
return { ip }
}
The useFetchIp
function returns null
. I thought I may not fetching the IP so, I got the value of ipDetail
which was { "ip": "5.126.121.39" }