Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onUpdate doesn't work #167

Closed
sabba88 opened this issue Jan 22, 2020 · 2 comments
Closed

onUpdate doesn't work #167

sabba88 opened this issue Jan 22, 2020 · 2 comments

Comments

@sabba88
Copy link

sabba88 commented Jan 22, 2020

Hi,
I try the pagination example in my App but the fetch is done one time only on onMount. When I update page state nothing is done. Checking the Network tab in Google Chrome DevTools any other request is done.
This is my code

export default function Reservation() {
  const [page, setPage] = useState(1);

  const { loading, error, data: reservations } = useFetch(
    {
      path: `${endpointShuttles}?page=${page}`,
      onNewData: (currReservations, newReservations) => [...currReservations, ...newReservations.data],
      data: [],
    },
    [page],
  ); // onMount AND onUpdate whenever `page` changes

  // Used to test if useEffect is fired
  useEffect(() => console.log('useEffect page', page), [page]);

  return (
    <>
      <h1>Pagination</h1>
      {error && 'Error!'}

      <ul>{reservations && reservations.map((user, i) => <li key={i}>{user.shuttleCode}</li>)}</ul>
      <button type="submit" onClick={() => setPage(page + 1)}>
        {loading ? 'Loading...' : 'Load Data'}
      </button>
    </>
  );
}

The server return 200 with the correct data.
The useEffect added to test if page change is fired and console.log woks fine.
In upper component I use Provider with some options and custom interceptors but in the request interceptor the console.log is fired only at onMount.

It seems that the change of page state is not triggered.

Someone have any idea?

Thanks in advance,
Samuele

@alex-cory
Copy link
Collaborator

Please make a codesandbox. Here is a template

@sabba88
Copy link
Author

sabba88 commented Jan 23, 2020

Hi Alex,
thanks for your reply. Putting the code in codesandbox everything works fine. After some test the problem was just the version of the library. I have installed the 0.2.1 version, I upgrade to 0.2.5 and everything is ok.
In the realeases note this function is added in the version 0.2.1 maybe there was some issue that was solved in newer versions.

Samuele

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants