import { useState, useEffect } from "react";
import axios from "axios";
import { useParams } from "react-router-dom";
const Post = () => {
const params = useParams();
const [post, setPost] = useState(null);
useEffect(() => {
fetchPost();
}, []);
const fetchPost = () => {
axios
.get(
`https://api.example/v1/json/post?post=${params.postSlug}`
)
.then((response) => {
setPost(response.data.data.post);
})
.catch((error) => console.log({ error }));
};
return post ? (
<div className="singlePost">
<div className="content" key="content">{post.content}</div>
</div>
) : null;
};
export default Post;
This is my whole component and API is developed on laravel. If I change the backend data from db React client does not pull in the new content while if I paste the endpoint in a browser or Postman, it does display updated content. I know that I can just add a timestamp at the end of url to force frontend to pull new data everytime but that'll effect the efficiency of application. Can someone explain the reason and solution to this issue without using any cache burst (i.e. timestamp in url or 'no-cache' in axios headers).
Thanks in advance