My app has a modal with a few buttons. When the buttons are clicked, calls are made to the server, which work as expected. However, the screen does not refresh with the changes and I can't figure out how to make it work. I think I need to update the data, which will cause the components to render again, which is why I'm trying to use refetch. FYI - I found this, which helps a lot, but I'm not sure what's wrong with my implementation.
...
import { GetData } from '../../Api/GetData';
...
export const Timeline = ({props}) => {
const [refresh, setRefresh] = useState(true)
const {isLoading, isSuccess, data, refetch} = useQuery("key",
GetData({name: props.name, period: props.period}), {
staleTime: Infinity,
enabled: false
})
useEffect(() => {
if(refresh) {
setRefresh(false)
refetch()
}
}, [refresh, refetch]) // refresh is set to true by child components
if(isLoading) ...
}
export const GetData = async (params) => {
const baseUrl = process.env.REACT_APP_ESPO_BASE_URL
const url = "https://..."
const headers = {"X-Api-Key": process.env.REACT_APP_API_KEY}
const response = await fetch(url, {method: "GET", headers: headers})
return response.json()
}
Any help would be greatly appreciated.
props.nameandprops.periodso you might want to add them to thequeryKeyarray as dependencies. That way, whenever one of those values change, your query will automatically update. You will need to setenabledtotruethough.['key']).