TinaSky
TinaSky

Reputation: 39

How to use useEffect in a table to fetch data from backend

I am creating a table which looks like this:

    Id  number of books
    1       XX
    2       ...
    3       ...
   ...      ...

The number of books would be fetched from backend using useEffect

  useEffect(() => {
    if (Id) {
      dispatch(
        getBookNumbers({
        Id: Id,
        })
      );
    }
  }, []);

How can I write useEffect functions to fetch different numbers through different Ids? Thanks so much for your time and help!

Upvotes: 0

Views: 197

Answers (1)

Vahid Alimohamadi
Vahid Alimohamadi

Reputation: 5868

Just set Id variable as a state variable. Second array param of UseEffect, will create it sensitive on those variables, which means it will trigger on their change. So your useEffect can be like this:

useEffect(()=>{
   if !Id return;
   // do my logic
},[Id])

React has a clear documentation, It will help you understand well.

Upvotes: 1

Related Questions