Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

239
Visualizações
When a user clicks on a related blog, Reactjs requires them to refresh page

I have React pages (BlogContent & RelatedBlog),

When a user views blog content, he can see below more blogs for the author. When the user clicks on the article he wants to read, the slug on Url changes but the content does not change until the user refreshes the page, so the user must refresh the page every time he wants to see all related blogs for the author.

I think the reason the blog page require refreshes every time is because useEffect() , but I don't know what is the trick to fix it

Here's my code to page BlogContent:

export const BlogContent = () => {

const { slug } = useParams();


const [data, setData] = useState({ posts: [] });

useEffect(() => {
    axiosInstance.get(slug).then((res) => {
        setData({ posts: res.data });
    });
}, [setData]);


let AuthorBlog = data.posts.author

};

and here for RelatedBlog

export const RelatedBlog = (props) => {

const { AuthorBlog } = props;


const [appState, setAppState] = useState([]);
useEffect(() => {
    axiosInstance.get("/").then((res) => {
        const allBlogs = res.data;
        setAppState(allBlogs);
    });
}, [setAppState]);

const filterAuthor = appState.filter((item) => item.author === AuthorBlog) ;

Thanks

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

As I've written in the comments - adding slug to the dependency array of your useEffect fixed the problem.

Why is that? Dependency array in useEffect hook is used to rerun the hook when one of these variables changes. In your case, changing the url, changes the slug so the component should load new data.

I would even remove setData from this array, because it doesn't do anything as useState setters don't ever change.

useEffect(() => {
  laxiosInstance.get(slug).then((res) => {
    setData({ posts: res.data });
  });
}, [slug]);
about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda