question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Error when render <Menu.Button> as a custom component

See original GitHub issue

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.4.1

What browser are you using?

Chrome

Reproduction repository

https://codesandbox.io/s/wonderful-leaf-t4899?file=/src/App.js

Describe your issue

When trying to use <Menu.Button /> with a custom component it doesn’t work as expected and I get the following warning: Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Simplified example:

const CustomButton = ({ children, onClick }) => (
  <button onClick={onClick}>{children}</button>
);

<Menu.Button as={CustomButton}>More</Menu.Button>

Does headless-ui require the <CustomButton/>-component to forward unknown props down to the html <button> element am I using it wrong?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
RobinMalfaitcommented, Mar 23, 2022

When you are adding custom components, make sure to passthrough all the props you get from Headless UI. These include event handlers, but also aria related attributes.

E.g.:

// This is incorrect
const CustomButton = ({ children, onClick }) => (
  <button onClick={onClick}>{children}</button>
);

<Menu.Button as={CustomButton}>More</Menu.Button>
// This is correct
const CustomButton = React.forwardRef((props, ref) => (
  <button ref={ref} {...props} />
));

<Menu.Button as={CustomButton}>More</Menu.Button>
1reaction
adamlindqvistcommented, Mar 24, 2022

When you are adding custom components, make sure to passthrough all the props you get from Headless UI. These include event handlers, but also aria related attributes.

E.g.:

// This is incorrect
const CustomButton = ({ children, onClick }) => (
  <button onClick={onClick}>{children}</button>
);

<Menu.Button as={CustomButton}>More</Menu.Button>
// This is correct
const CustomButton = React.forwardRef((props, ref) => (
  <button ref={ref} {...props} />
));

<Menu.Button as={CustomButton}>More</Menu.Button>

It might be worth mentioning this in the documentation?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding menu button for Navigation drawer gives error
contentComponent is basically React Component, where you display list ... Sometimes this gives error because of undefined navigation object.
Read more >
User error on rendering second level menu items with Layout ...
Basically I'm trying to recreate the blog post seen here https://www.phase2technology.com/blog/creating-mega-menu-layout-builder.
Read more >
Custom Button giving error in lightning record page
When I am clicking on cutombutton in lightning it's giving me the below error:- List has no rows for assignment to SObject
Read more >
A button in the command bar is not working correctly
Several factors can cause a button action to fail. These include invalid ribbon customizations in which the button's associated command ...
Read more >
[Solved] UI Button not working at all - Unity Forum
If anyone else will have a similar problem, select your Canvas GameObject and look at it's components. I had the following: Canvas, Canvas ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found