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

Fetch runs double with old and updated value

I have a fetch function as a component:

export const FetchBooksBySubject = (selectedValue) => {
    const options = {
      method: `GET`,
    };
    return fetch(`${server}/books?subjects_like=${selectedValue}`, options)
    .then((response) => {
      if(response.ok){
        return response.json()
      }
        throw new Error('Api is not available') 
      })
    .catch(error => {
      console.error('Error fetching data: ', error)
    })
}

This function I use in my App.js, where the idea is, to run fetch every time, the value gets updated:

useEffect(() => {
   if(selectedValue) {FetchBooksBySubject(selectedValue).then(data => setBookList(data))};
  }, [selectedValue])

   
  const handleChange = e => {
    setSelectedValue(e);
    FetchBooksBySubject(selectedValue);
    
  };

So it works, but it runs a doble request basically when I set the new value. Firts is for the value before the update and second for the updated value. Why? And is there any chance to run it only for the updated value?

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

>Solution :

First, FetchBooksBySubject is not a valid function component. Component should return React element.

const element = <h1>Hello, world</h1>;

FetchBooksBySubject is just a function returns a Promise, so you should rename it like fetchBooksBySubject.

Second, it is natural that your FetchBooksBySubject runs twice.
The first time is when the selectedValue changes. Check out official document about useEffect. When you provide some value in your dependency array, the values’s change will trigger useEffect to run again.

useEffect(() => {
   if(selectedValue) {FetchBooksBySubject(selectedValue).then(data => setBookList(data))};
}, [selectedValue])

The second time when fetching is called is after setSelectedValue, you are calling fetch function manually. So delete it if you don’t need it.

const handleChange = e => {
    setSelectedValue(e);
    FetchBooksBySubject(selectedValue); // function called manually
};
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