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

108
Views
call again a api when id is passed in react js

i have this callback funtion inside useeffect i want to pass differnt props.id from parent js to this file so i get differnt datas the problem is const res = await axios.get("messages/get-all-messages/?receiver=" + props.id, config ); does not get recalled when i pass a new props.id from parent.js how can i call again this api when i pass a new props.id


 function chat(e) {
    e.preventDefault()

    const config = {
      headers: {
        Authorization: `token ` + localStorage.getItem("token"),
      },
    };
    const data = { receiver: props.id, message: inputField };
    axios
      .post("messages/send-message/", data, config)
      .then((res) => {
        getData();
      })
      .catch((error) => {
        console.log(error);
      });
  }
  const getData = useCallback(async () => {
    const config = {
      headers: {
        Authorization: `token ` + localStorage.getItem("token"),
      },
    };
    const res = await axios.get(
      "messages/get-all-messages/?receiver=" + props.id,
      config
    );
undefined which will crash.
    if (res.status === 200) {
      if (mountedRef.current) {
      
        setdata(res.data.);
        
      }
    }
  }, []);

useEffect(() => {
    if (mountedRef.current) {
      getData();
    }
    return function cleanup() {
      mountedRef.current = false;
    };
  }, [getData]);


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

0

useCallback(fn, deps)

useCallback will return a memoized version of the callback that only changes if one of the dependencies has changed. Therefore you should have been added props.id as dependencies to useCallback hooks

const getData = useCallback(async () => {
   // do something
}, [props.id])
about 4 years ago · Juan Pablo Isaza Report

0

You can add props.id to the useEffect call, anything you pass as the an argument in the array will ensure that useEffect is called. Therefore anytime props.id updates the logic in the useEffect will execute.

"React compares the current value of dependency and the value on previous render. If they are not the same, effect is invoked."

Helpful Source:https://dev.to/nibble/what-is-useeffect-hook-and-how-do-you-use-it-1p9c

useEffect(() => {
if (mountedRef.current) {
  getData();
}
return function cleanup() {
  mountedRef.current = false;
};
  }, [getData, props.id]);
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!