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

128
Visualizações
How to add ```Context``` with routes set up?

I want to add the searchContext context in my App.js so that my Navbar an Results components have access to the variables in it. How would I do this with my routes set up? I tried to just add with with them but that didn't work. This is the code.

<Router>
        // Where does <searchContext.Provider> go?
        <Navbar></Navbar>
        <Routes>
            <Route exact path='/' element={<Home />} />
            <Route path='/results'>
              <Route path='/results/:value' element={<Results />} />            
              {/*<Route path='/results/:value/:slot' element={<Slot />}*/}
            </Route>
            // Another route that has nothing to do with searchContext
         </Routes>
    </Router>

Edit: Added "// Another route that has nothing to do with searchContext"

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

0

if i were you i would just wrap the all thing with the provider as long as its not to heavy.

about 4 years ago · Juan Pablo Isaza Relatório

0

Hellow, this goes with general understanding of context Api, now the structure of your folders is your choice but i'll be very brief here

//on your context file searchContext.js
import {useState,createContext} from 'react';

export const SearchContext = createContext(null);
export const ContextProvider = props => {
  const [search,setSearch] = useState(null);
  

 return <SearchContext.Provider value = {{search,setSearch}}>
        {children>
        </SearchContext.Provider>
}

//now on your route.js
import { ContextProvider} from '../your/path/file';
....
<Router>
        // Where does <searchContext.Provider> go?
        <ContextProvider>
        <Navbar></Navbar>
        <Routes>
            <Route exact path='/' element={<Home />} />
            <Route path='/results'>
              <Route path='/results/:value' element={<Results />} />            
              {/*<Route path='/results/:value/:slot' element={<Slot />}*/}
            </Route>
            // Another route that has nothing to do with searchContext
         </Routes>
      </ContextProvider>
    </Router>
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