Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

215
Views
react-router v5.1.0 No se puede leer la propiedad 'ubicación' de undefined ,

Estoy tratando de cambiar el color de fondo de mi contenedor en función de la URL de la página, por lo que si el usuario navega a la URL '/movie' , debería cambiar el fondo, por ejemplo, a rojo; de lo contrario, debería establecer el fondo en verde.

Aquí está mi index.js

 import React from 'react'; import { BrowserRouter, Switch, Route, useLocation} from 'react-router-dom'; import styled from 'styled-components'; import Movies from 'pages/Movies/Movies' import Templates from 'pages/Movies/Templates' ; export default () => { const location = useLocation(); return ( <> <BrowserRouter> <Container style={{backgroundColor:location.pathname === '/movies' ? 'red' : 'green'}}> <Main> <App> <Switch> <Route path='/templates' component={Templates} /> <Route path='/movies' component={Movies} /> </Switch> </App> </Main> </Container> </BrowserRouter> </> ); } const Container = styled.div` min-height: 100vh; `;

Desafortunadamente, recibo el siguiente error

 Uncaught TypeError: Cannot read property 'location' of undefined at useLocation (app.js:54283) at app.js:72792 at renderWithHooks (app.js:37714) at mountIndeterminateComponent (app.js:40129) at beginWork$1 (app.js:41478) at HTMLUnknownElement.callCallback (app.js:21756) at Object.invokeGuardedCallbackDev (app.js:21805) at invokeGuardedCallback (app.js:21860) at beginWork$$1 (app.js:47124) at performUnitOfWork (app.js:46032)

¿Qué debo hacer para solucionar este problema?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe mover BrowserRouter fuera de ese componente. Lo mejor es moverlo a index.js y adjuntar <App />

over 4 years ago · Santiago Trujillo Report

0

Está utilizando useLocation fuera de los componentes envueltos por el enrutador, en su caso, el componente raíz (dentro app.js ) es el envoltorio del componente del enrutador.

verifique esto , podría ver la diferencia cuando usa ese gancho en los componentes App.tsx y QueryReducer .

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!