Reputation: 1333
I am trying to fetch data from an API which requires access_token
. There is another API for fetching access_token
. So, I am first trying to fetch acess_token and then pass it to the API which returns data.
I observed that the token API is getting hit but by the time it is passed to the data API, token is undefined
Please suggest how to get access_token first and then pass it to data_api.
Upvotes: 0
Views: 198
Reputation: 758
const useFetch = (url,headers)=>{
const [status, setStatus] = useState('idle');
const [data, setData] = useState([]);
const [loading, setLoading] = useState(true)
useEffect(() => {
const fetchData = async () => {
setStatus('fetching');
const response = await fetch(
url,headers
);
const data = await response.json();
setData(data)
setLoading(false)
setStatus('fetched');
};
fetchData();
}, [url,headers,setStatus,setData,setLoading]);
return { data,loading }
}
Upvotes: 1