I have a little web application that uses axios to fetch some orders from the API. My problem is the new orders only appear when the page is refreshed, they do not update automatically.
Here is my useEffect hook:
useEffect(() => {
setLoading(true);
apiClient
.getEvents()
.then((res) => {
console.log(res);
setOrders(res.data);
setLoading(false);
})
.catch((err) => {
console.log(err);
});
}, []);
And here is where I use axios:
import axios from "axios";
const username = "user";
const password = "pass";
const token = Buffer.from(`${username}:${password}`, "utf8").toString("base64");
const apiClient = axios.create({
baseURL: "API URL",
withCredentials: false,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Basic ${token}`,
},
});
export default {
getEvents() {
return apiClient.get("/orders");
},
getEvent(order_id) {
return apiClient.get("/orders/" + order_id);
},
};
useEffect? It would be better if we could see how the component is declaring "data" and using it.console.log(res)log what you expect? Is the page stuck "loading", or does thesetLoading(false);stop that? Can you be more specific about what data isn't updating?