[V2] innerRef doesn't work with styled(Component)
See original GitHub issueI 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
- Open the WebpackBin link.
- Open your developer console.
- 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:
- Created 6 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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
And invoked it like this
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.