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.

[V2] innerRef doesn't work with styled(Component)

See original GitHub issue

I am trying to set the ref for a <Input /> styled components.

The component has been created with const Input = styled(InputUnstyled).

I have the following react app:

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

class InputUnstyled extends React.Component {
  render() {
      var _props = this.props,
          isOpen = _props.isOpen,
          rest = _objectWithoutProperties(_props, ["isOpen"]);

      return React.createElement("input", rest);
  }
}

const Input = styled(InputUnstyled)`
  font-size: 1.5em;
  text-align: center;
  color: palevioletred;
`;

const Wrapper = styled.section`
  padding: 4em;
  background: papayawhip;
`;

export default class App extends React.Component {
  
  constructor(props) {
    super(props);
    this.handleIsOpen = this.handleIsOpen.bind(this);
    this.state = { isOpen: false };
  }
  
  componentDidUpdate() {
     this.handleFocus(); 
  }
  componentDidMount() {
     this.handleFocus(); 
  }
  
  handleIsOpen() {
     this.setState({ isOpen: !this.state.isOpen }); 
  }
  
  handleFocus() {
     this.search.focus(); 
  }
  
  render() {
    return (
      <Wrapper>
        <button type="button" onClick={this.handleIsOpen}>Rerender</button>
        <Input type="text" isOpen={this.state.isOpen} innerRef={(search) => { this.search = search }} />
      </Wrapper>
    );
  }
}

Version

2.0.0-8

Reproduction

https://www.webpackbin.com/bins/-Kg-9xQ8QPTOnSFLmOOt

Steps to reproduce

  1. Open the WebpackBin link.
  2. Open your developer console.
  3. Click on the button to trigger this.search.focus().

It will through an error in your developer console.

Expected Behavior

The <Input /> element should receive the :focus.

Actual Behavior

Throw error this.search.focus is not a function

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
magnumnighthawkcommented, Oct 9, 2017

@philpl Thanks … That worked for me. In my case, I was trying to get the reference of a third party component, react-custom-scrollbar. I managed to do it in the following way

import Scrollbars from 'react-custom-scrollbars';

export const StyledScroll = styled(({ _ref, ...rest }) =>  <Scrollbars ref={_ref} {...rest} />)`
  /* Styles */
`;

And invoked it like this

<StyledScroll _ref={(el) => { this.scrollbar = el; }} />
4reactions
kittencommented, Mar 25, 2017

This is not a bug, but due to how the “innerRef” logic works. Once it’s in the input props, it will pass it innerRef as ref to the underlying element , I.e. The unstyled component in your example.

Thus the component that renders Input will receive a ref, but it won’t be the element, but the unstyled component itself. If you want the element ref then either add your own ref prop and set it on the component instance, so that you can access it with its ref, or use findDOMNode.

Read more comments on GitHub >

github_iconTop Results From Across the Web

styled components - innerRef getting completely ignored
If I log the props being passed into the styled component, I can see innerRef. But if I put a log in my...
Read more >
Advanced Usage - styled-components
styled -components has full theming support by exporting a <ThemeProvider> wrapper component. This component provides a theme to all React components ...
Read more >
Styled-components v4 not compatible with Atlaskit
The “innerRef” API has been removed in styled-components v4 in favor of React 16 ref forwarding, use “ref” instead like a typical component....
Read more >
Styled Component innerRef - CodeSandbox
Styled Component innerRef. 0. Embed Fork Create Sandbox Sign in. Sandbox Info. Styled Component innerRef. 0. 427. 2. achanta3215achanta3215.
Read more >
[Solved]-How to use the styled-component property innerRef ...
[Solved]-How to use the styled-component property innerRef with a React stateless ... score:2. import Button from './Button.js'; class Foo extends React.
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