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

545
Visualizações
Why doesn't react-router-dom useParams hook work within Context?

I've got a context shared amongst a group of profile pages. The context is responsible for loading and setting the user's profile from a database, like so:

const Profile = props => {
    const { userProfile } = useContext(ProfileContext);
    return userProfile && (
        <div className="profile-container">
            ...stuff
        </div>
    );
};

export default Profile;

...routes:

<BrowserRouter>
    <Header />
    <main className="main-container">
        <Switch>
            ...other routes
            <ProfileContextProvider>
                <Route path="/profile/:id" exact component={Profile} />
                <Route path="/settings" exact component={Settings} />
            </ProfileContextProvider>
        </Switch>                       
    </main>
    <Footer />
</BrowserRouter>

The context itself is very simple:

import React, { useState, useEffect } from "react";
import { useParams } from "react-router-dom";

export const ProfileContext = React.createContext({
    userProfile: {},
    setUserProfile: () => {}
});

ProfileContext.displayName = "ProfileContext";

const ProfileContextProvider = props => {
    const { id } = useParams(); //always undefined!
    const [userProfile, setUserProfile] = useState(null);

    useEffect(() => {
        loadData();
    }, []);
    
    const loadData = async () => {
        ...api call to load data
    };
    
    return (
        <ProfileContext.Provider value={{ 
            userProfile,
            setUserProfile
        }}>
            {props.children}
        </ProfileContext.Provider>
    );
};

export default ProfileContextProvider;

However, that use of useParams() in there doesn't work. The "id" is always undefined. If I move this useParams() usage to the Profile component itself, it works fine. This does me no good, because I need to make a decision about the route, when loading data in the context, before I load all of Profile. I need it to work in the context!

Is this possible? Am I doing something wrong?

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

0

Context is itself different management and query param should props from the page to context set variable then it works

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