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

add ability to pass href to sub components #29

Open
madhums opened this issue Sep 17, 2023 · 0 comments
Open

add ability to pass href to sub components #29

madhums opened this issue Sep 17, 2023 · 0 comments

Comments

@madhums
Copy link

madhums commented Sep 17, 2023

I'd like to be able to pass props to child components viz

const itemTypeToComponent = {
  [ITEM_TYPES.PAGE]: PageLink,
  [ITEM_TYPES.ELLIPSIS]: Ellipsis,
  [ITEM_TYPES.FIRST_PAGE_LINK]: FirstPageLink,
  [ITEM_TYPES.PREVIOUS_PAGE_LINK]: () => null,
  [ITEM_TYPES.NEXT_PAGE_LINK]: () => null,
  [ITEM_TYPES.LAST_PAGE_LINK]: LastPageLink
};

Right now I use a hook within each of these. For example:

// Custom hook to create a href without loosing any query params of the page
// Takes queryparam and it's value as an argument
function useQueryParam(name, value) {
  const pathname = usePathname();
  const searchParams = useSearchParams();
  const params = new URLSearchParams(searchParams);
  params.set(name, value);
  const href = pathname + '?' + params.toString();
  return [href];
}

const PageLink = ({ onClick, isActive, value }) => {
  const [href] = useQueryParam('page', value);         // <------------- using a hook here to get the href
  return (
    <li className="page-item">
      <a
        className={clsx('page-link', isActive && 'disabled')}
        href={href}
        onClick={withPreventDefault(onClick)}>
        {value}
      </a>
    </li>
  );
};

const Ellipsis = ({ onClick, isActive, value }) => {
  const [href] = useQueryParam('page', value);         // <------------- using a hook here to get the href
  return (
    <li className="page-item">
      <a
        className={clsx('page-link', isActive && 'disabled')}
        href={href}
        onClick={withPreventDefault(onClick)}>
        ...
      </a>
    </li>
  );
};

It would be cool if a href can be generated and passed through. Would it be possible? Has anyone been using this library with a href?

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

1 participant