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

132
Views
React No se puede leer la propiedad 'props' de undefined en React leyendo la variable de ruta de identificación de React Router

Estoy tratando de pasar una variable de ruta, ID de usuario, como apoyo a un componente de React representado por React Router:

Perfil.js

 export const Profile = (props) => { //array of compatible users fetched for a user. const [userProfileInformation, setUserProfileInformation] = useState([]); const [isLoading, setLoading] = useState(true); useEffect(() => { getUserProfileInformation().then(() => { setLoading(false); }); }, []); const getUserProfileInformation = async () => { //Currently hardcoded value until authorisation implemented. const userId = this.props.match.params.id; const response = await UserService.getUserProfileInformation(userId) .then(response => response.json()) .then(data => { setUserProfileInformation(data); }); } if (isLoading) { return ( <div id="loading"> <h2>Loading...</h2> </div> ) } return ( <div> <div className="profileCard"> <h2>{userProfileInformation.email}</h2> </div> </div> )

}

El error ocurre debido al código 'userId = props.match.params.id'.

Aplicación.js:

 const root = ReactDOM.createRoot( document.getElementById("root")

);

 root.render( <BrowserRouter> <Routes> <Route path="/" element={<App />} /> <Route path="/profile/:userId" element={<Profile />} /> </Routes> </BrowserRouter> );

Tablero.js:

 export const Dashboard = () => { return( <div> <h2>Dashboard</h2> <Button color="link"><Link to={`/profile/1`}>Profile</Link></Button> </div> );

}

Lo anterior usa temporalmente un valor codificado para el enlace, 1.

Editar:

Probado con const userId = props.match.params.id; y const userId = this.props.match.params.id y const userId = props.match.params.userId; y const userId = this.props.match.params.userId;

Todos dan el mismo resultado que antes.

Gracias

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La versión de react-router-dom que está utilizando es v6. En esta versión (que tiene muchos cambios importantes a los que debe estar atento), no se puede acceder al objeto params ya que las propiedades de route props no se pasan al element /componente.

Para acceder al userId de usuario, ahora necesita usar el useParams .

  1. Importarlo: import {useParams} from 'react-router-dom'
  2. En lugar de const userId = props.match.params.userId , use const { userId } = useParams() y luego utilícelo como lo hizo antes.

Si esta respuesta te ayuda, por favor acéptala. ¡Gracias!

about 4 years ago · Juan Pablo Isaza 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!