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

forwardRef for button component #341

Closed
hotrush opened this issue Aug 23, 2022 · 3 comments · Fixed by #391
Closed

forwardRef for button component #341

hotrush opened this issue Aug 23, 2022 · 3 comments · Fixed by #391
Labels
help wanted Extra attention is needed 🚀 enhancement New feature or request

Comments

@hotrush
Copy link

hotrush commented Aug 23, 2022

Is your feature request related to a problem? Please describe.

Seems it's not possible to use buttons with Link component at Next.js

import { Button } from 'flowbite-react'
import Link from 'next/link'

<Link href="/page" passHref>
  <Button>
    Go somewhere
  </Button>
</Link>

This code throws a warning

Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Would be good to add forwardRef for button component to increase compatibility with next.js

Describe the solution you'd like
Solution is described at docs https://nextjs.org/docs/api-reference/next/link#if-the-child-is-a-functional-component

@rluders
Copy link
Collaborator

rluders commented Aug 24, 2022

I'm wondering if this one can't be solved by implementing the as= component modifier.

What do you think @tulup-conner ?

@rluders rluders added 🚀 enhancement New feature or request help wanted Extra attention is needed labels Sep 18, 2022
@fs-innonova
Copy link

I had a look into the source code, and I guess in order to achieve this we would have to update this part

const Component = isLink ? 'a' : 'button';

and if is a button we need to add: ref={ref}

Here there is some more info on how to use this forwardRef

@myabeaver
Copy link
Contributor

There is a workaround using a HoC and copying displayName, defaultProps and propTypes with Object.assign.

import type { NextPage } from 'next';
import Link from 'next/link';
import { type FC, type ForwardedRef, forwardRef, LegacyRef } from 'react';
import { Button } from 'flowbite-react';

const withRef = <T, P = {}>(
    render: FC<P & { ref?: LegacyRef<T> | undefined }>
) =>
    Object.assign(
        forwardRef<T, P>((props, ref) => render({ ...props, ref })),
        render
    );

const ButtonWithRef = withRef(Button);

const Home: NextPage = () => {
    return (
        <Link href="/" passHref>
            <ButtonWithRef>Hello World</ButtonWithRef>
        </Link>
    );
};

export default Home;

@rluders If you want, I can provide a PR tomorrow to wrap the component with forwardRef.

myabeaver pushed a commit to myabeaver/flowbite-react that referenced this issue Oct 16, 2022
Added forwardRef to button component to work with Next.js' Link component.

fix themesberg#341
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed 🚀 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants