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.

Heading does not forward ref

See original GitHub issue

Heading component should pass down ref, as documented in https://github.com/grommet/grommet/issues/2015

Expected Behavior

<Heading ref={...}> should forward the ref to it’s underlying DOM-node as described in #2015

Actual Behavior

ref it’s not forwarded to the underlying DOM-node (its styled-component for this purpose). Hence, when you do posed(Heading), it crashes. The correct behaviour is implemented in Box, or in other words, posed(Box) works just fine.

URL, screen shot, or Codepen exhibiting the issue

Check out the following codepen: https://codesandbox.io/s/zen-thunder-xiuiv Make sure to try

  1. posed(Heading) + Click Button -> The problem
  2. posed(Box) + Click Button -> The correct behavior

Steps to Reproduce

  1. Use <Heading ref={...} >
  2. Verify, the ref you receive is the underlying headline

Your Environment

Master branch

Solution

Note: I have not tried, if this works, but I suppose it does, as it does in Box

In file https://github.com/grommet/grommet/blob/master/src/js/components/Heading/Heading.js#L32 change const HeadingWrapper = HeadingDoc || Heading; to

const HeadingWrapper = compose(
  withForwardRef,
)(HeadingDoc || Heading);

as seen in https://github.com/grommet/grommet/blob/master/src/js/components/Box/Box.js#L114

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Zyclotrop-jcommented, Jul 18, 2019

Hi @ShimiSun,

I had a look and it seems there are underlying problems that might make this difficult.

I tried in https://github.com/Zyclotrop-j/grommet/commit/5398b27ddd4c07249f8f234b4aabddcbbbb1452d. While the forwardRef actually works (also see first example https://reactjs.org/docs/forwarding-refs.html) recompose seems to do some funny stuff (see https://github.com/Zyclotrop-j/grommet/blob/68c62c0e3fcf0215433dd4abcebebac4ebf5c63d/src/js/components/__tests__/__snapshots__/components-test.js.snap#L1975).

I think some further testing is required to ensure this actually works in all situations, but I didn’t have the time to do that yet.

As a sidenote, @oorestisime, refs don’t work when put on functional components, but they do work in functional component, and in particular the forward-ref-hoc don’t even pass ref, but forwardRef, so no worries here that will just work fine 😃

0reactions
jcfilbencommented, Mar 3, 2021

I think this issue can be closed since #4371 has been merged

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is ForwardRef causing error when using pair tag
The solution, react.forwardRef takes two generic arguments, the first is the type of DOM element your will be forwarding the ref too, and...
Read more >
Forwarding Refs - React
Ref forwarding is an opt-in feature that lets some components take a ref they receive, and pass it further down (in other words,...
Read more >
How to use React forwardRef - DEV Community ‍ ‍
I've implemented my ref forwarding with useRef hook and function components the same way you did, but I'm still getting inputRef.current is ......
Read more >
UseRef, CreateRef, ForwardRef? What's up with refs in React?
You'll get the error: "Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use...
Read more >
API Reference - styled-components
Returns a StyledComponent that does not accept children. Place it at the top of your React tree and the global styles will be...
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