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

usePagination / usePaginator #55

Open
cloudever opened this issue Oct 31, 2018 · 5 comments
Open

usePagination / usePaginator #55

cloudever opened this issue Oct 31, 2018 · 5 comments
Labels
enhancement New feature or request

Comments

@cloudever
Copy link
Contributor

What about that possible hook?

type HookProps =  [number, (position: number): number, ?number];

type HookParams = {
    pageSize: number, 
    collectionSize: number,
    initialCurrentPage: ?number = 1,
}

function ListComponent() {
    const params: HookParams = { 
        pageSize, 
        collectionSize,
        initialCurrentPage,
    };

    const [currentPage, gotoPage, totalPages]: HookProps = usePaginator(params);
  
    const gotoNext = (): number => gotoPage(currentPage + 1)
    const gotoPrev = (): number => gotoPage(currentPage - 1)

    return (
        <Pagination {...} />
    )
}
@streamich
Copy link
Owner

I think we could do something like usePagination, but currently the API seems to be quite verbose to me. Maybe it makes sense to first start with a "simpler" cursor based pagination, where you receive an opaque cursor from the server to be used in next request.

const ListComponent = () => {
  const [list, loadMore] = useCursorPagination(
    (cursor) => `http://my-site.com/list.php?cursor=${cursor}`,
    (response) => [response.body.list, response.body.cursor],
  );

  return (
    <>
      {list.map(({item, key}) => <Item {..item} key={key} />)}
    </>
  );
};

@wardoost wardoost added the enhancement New feature or request label Mar 31, 2019
@wardoost wardoost mentioned this issue Apr 6, 2019
@SnowFlowers
Copy link

In modern web app,We use rest-api for our data,I think cursor is a better description, but if i compose cursor and response in usePagination is not uneconomic. Most of us don't need your api in this component.What we should do is only show the Pagination UI.like this
image,

#210 (comment)
This is just for pagination. Not for server data.

@wardoost
Copy link
Contributor

wardoost commented Apr 6, 2019

@SnowFlowers I'm not in favor of adding UI components to this library as this is a hooks library. The hook should expose state and callbacks that can be used with any UI components.

I think @streamich's example should maybe be called useAsyncMore and have an API that's similar to the useAsync hook. @cloudever's hook could be simplified to something like this:

const {nextPage, prevPage, goToPage} = usePagination(initialPage);

This is so simple and similar to useCounter that I'm not really sure if it's worth adding a custom hook to the library for this.

@SnowFlowers
Copy link

@wardoost OK, I see. Maybe it's not a good part for this repo.

@slykar
Copy link

slykar commented May 11, 2020

This is so simple and similar to useCounter that I'm not really sure if it's worth adding a custom hook to the library for this.

The first thing I did when I found this repo was ctrl+f -> usePaginat.... It could be just an alias. I would say that name aliasing is a pretty common approach to make the API more developer friendly. How many new users will figure out that they can do pagination with useCounter?

Anyone can do the aliasing on their own, but they could also implement their own useCounter hook. It's all simple, but it's nice to have a little bit less work to do.

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

No branches or pull requests

5 participants