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

140
Visualizações
React no rerender after url change

My route definition:

...{
path: '/profile/:id',
component: ProfilePage
}, ...

and then the mapping is:

{routes.map(({ path, component }) => {
        return (
            <Route
                exact
                key={path}
                path={path}
                component={component}
            />
        )
    })}

If I navigate from */main* to */profile/3* everything is fine. But if I am already on a profile and navigate to another profile with different ID (*profile/3* -> *profile/5*) the problem starts. The URL in the browser changes but the content does not rerender. Also the useEffect function is not called again. If I navigate to any other url and then start the search for a person, everything is fine again. So only a second call from profile to profile does not work.

My navigation in the list is done by

const onItemSelected = (event) => {
    const selectedPerson = event.itemData
    setShowSuggestions(false)
    setFilteredSuggestions([])
    setInput("")
    if(selectedPerson && selectedPerson.id)
      history.replace(`/profile/${selectedPerson.id}`);
}

My useEffect in profile.js

export default (props) => {
...
useEffect(() => {
    try {
        setLoading(true)
        const urlPath = window.location.href.split('/')
        const id = urlPath[urlPath.length - 1]
        loadProfile(id)

    } catch (e) {
        setLoading(false)
        history.push("/aufgabenliste")
    }

}, []);

I also tried history.push() but it didn't work too.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found the solution. First of all it is much easier to load the ID from the props instead of accessing the window.location.href...

And the second step is to set the props as dependency in useEffect. So the final code looks like:

 useEffect(() => {
    try {
        setLoading(true)
        const id = props.match.params.id
        loadProfile(id)
    } catch (e) {
        setLoading(false)
        history.push("/aufgabenliste")
    }

}, [props]);
about 4 years ago · Juan Pablo Isaza 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