I create this custom hook in my React app. It should return a boolean
.
const useFetchResponse = (url: string) => {
const [isValid, setIsValid] = useState<boolean>(false);
useEffect(() => {
const fetchResponse = async () => {
const response = await fetch(url);
console.log(response);
const obj = await response.json();
if (response.ok) {
console.log(await response.json());
setIsValid(true);
}
return response;
};
fetchResponse().then((res) => res);
}, []);
return isValid;
};
export default useFetchResponse;
When I log const obj = await response.json();
it returns: {"keyName":"some=key"}
.
How do I create a condition to check if response.json()
has a key named keyName
?
Is that for example console.log('keyName' in obj) // true
?
Do you see more things which I can improve and refactor?