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

166
Views
Is it an incorrect practice to use URL params as a way to fetch data using react router in an react application?

In my app I have a Home page that has a child named Posts the routes are set up in the following manner:

<Route path='/' element={<Home />}>
  <Route path='/posts/popular' element={<Posts />} />
  <Route path='/posts/new' element={<Posts />} />
</Route>

I would like to set it up where if I am on the popular path then my api call will be:

axios.get('/posts?sort=-popular')

But if I am on new, then the call would be:

axios.get('/posts?sort=-createdAt')

The way I was thinking of implementing it was to make the second param into a selector like:

<Route path='/' element={<Home />}>
  <Route path='/posts/:sortBy' element={<Posts />} />
</Route>

// in my Posts component I would call useParams
const {sortBy} = useParams();

// then in useEffect
axios.get(`/posts?sort=-${sortBy})

But this feels off, like I am doing it wrong. What is a better way, if any, of implementing this functionality?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can pass props into the <Posts /> component in your Route.

Here's an example:

<Route path='/' element={<Home />}>
  <Route path='/posts/popular' element={<Posts sort="popular" />} />
  <Route path='/posts/new' element={<Posts sort="createdAt" />} />
</Route>

Then inside Posts you can use the prop to determine which call to make:

const Props = ({ sort }) => {

// then in useEffect
axios.get(`/posts?sort=-${sort})
over 4 years ago · Santiago Trujillo Report

0

What you did is ok, but it will make the component harder to reuse, also if you changed the path you will need to change also the component Posts. It’s better to add a new prop sortBy inside your component Posts and pass the prop inside your Route component.

<Route path='/' element={<Home />}>
 <Route path='/posts/popular' element={<Posts sortBy="popular" />} />
 <Route path='/posts/new' element={<Posts sortBy="new"/>} />
</Route>
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!