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.

innerRef does not work as expected

See original GitHub issue

As rightfully pointed out in https://github.com/jxnblk/rebass/issues/72#issuecomment-315534122, since 1.0.0 we should start using innerRef to get the reference as that’s the way it’s done in styled-components.

However innerRef doesn’t actually work as expected. I suspect this has to do with the fact that in src/create-component.js and src/hoc.js components are wrapped in a way that innerRef doesn’t get passed down to the children. Some details about this are explained in here: https://github.com/styled-components/styled-components/issues/927#issuecomment-309140340.

A minimal reproducible test case for this issue can be seen here:

import { Input, Button } from 'rebass'

class InputWrapper extends React.Component {

  constructor(props) {
    super(props)
    this.inputRef = null
  }

  render() {
    return (
      <div>
      <Input innerRef={ c => this.inputRef = c } />
      <Button onClick={() => this.inputRef.focus()}>Focus</Button>
      </div>
    )
  }

}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
DJTBcommented, Feb 22, 2018

I haven’t been able to get innerRef working with 2.0 either yet. However, using the <Ref /> component from Semantic UI React has solved this (at least temporarily) for me.

import { Ref } from 'semantic-ui-react';
import { Input, Button } from 'rebass';
/* ... */
<Button onClick={() => console.log(this.InputRef.value)}>Log Value</Button>
<Ref innerRef={(node) => { this.InputRef = node }}>
  <Input />
</Ref>

Perhaps a similar helper could be added to Rebass. https://github.com/Semantic-Org/Semantic-UI-React/tree/5a37f2899fcb048a01a6a51d078455f5eb9219df/src/addons/Ref

0reactions
jxnblkcommented, Jun 10, 2018

Closing this since I think #188 addresses this. Please open a new issue if not

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error "provided.innerRef has not been ... - Stack Overflow
I'm trying to test a component that has a Draggable from react-beautiful-dnd inside, using react-testing-library and make a snapshot, ...
Read more >
API Reference - styled-components
A function that receives the props that are passed into the component and computes a value, that is then going to be merged...
Read more >
How to use React's forwardRef function - Felix Gerschau
React's references don't always behave like you would expect them to. In this tutorial, you'll learn why and how you can use the...
Read more >
Reusing the `ref` from `forwardRef` with React hooks - ITNEXT
This will work exactly as expected. Honestly, I don't think that it should stay for a while, because the solution is quite big...
Read more >
Referencing - React Advanced Form - GitBook
There are two kinds of referencing - component and element references. ... innerRef will not work if the form element is returned by...
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