I'm trying to implement the simple pattern of:
GETlist- enter new item, save
- make
POSTrequest - make new
GETrequest for updated data
Starting with the code from here,
const useApi = () => {
const [data, setData] = useState({ hits: [] });
const [url, setUrl] = useState(
'https://hn.algolia.com/api/v1/search?query=redux',
);
useEffect(() => {
const fetchData = async () => {
const result = await axios(url);
setData(result.data);
};
fetchData();
}, [url]);
return [{ data }, setUrl];
}
it's easy enough to make the initial fetch on page mount
const [{ data }, doFetch] = useDataApi(
'https://hn.algolia.com/api/v1/search?query=redux',
{ hits: [] },
);
but what I don't understand is how to do a 'refresh' of the data. Since there's no stateful variable that determines if the hook should re-run, how can I manually invoke the hook on a button click?