Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Watch route params changes, re-fetch data on change

How can I react to route param changes? I want to re-fetch data for new params of my item.
For e.g whenever I move from /1 to /2 I want to fetch the data for id 2. How can I do this using router v6?

import './App.css';
import { useParams } from "react-router-dom";

function Item() {
  const { id } = useParams();

    return (
      <div className="test">
       test of nested route id: { id };
      </div>
    );
  }
  
  export default Item;
  

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can detect changes with useEffect

import './App.css';
import { useParams } from "react-router-dom";
import {useEffect} from "react"

function Item() {
  const { id } = useParams();

  useEffect(() => {
    //...your function goes here
  }, [id])

    return (
      <div className="test">
       test of nested route id: { id };
      </div>
    );
  }

export default Item;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading