N
N
Nekdev2022-01-08 18:24:34
React
Nekdev, 2022-01-08 18:24:34

How to rewrite such code on React Router V6?

There is such a code using the render function. In v6 react-router, these functions and component have been replaced with element . How then to rewrite this code to the new standard?

Now the console is getting an error: " Matched leaf route at location "/users/4" does not have an element. This means it will render an with a null value by default resulting in an "empty" page ."

The code:

<Route path='/users/:id' render={({match, location, history}) => {
                       const { id } = match.params;
                       return <UserDetails userId={ id } />}}/>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2022-01-08
@nekoguard

import { Routes, Route, useParams } from 'react-router-dom';

const UserDetailsPage = () => {
  const { id } = useParams();
  return <UserDetails userId={ id } />}}/>
}

<Route path='/users/:id' element={<UserDetailsPage />}/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question