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 example on documentation with styled()

See original GitHub issue

I am trying the following code on the documentation

https://www.styled-components.com/docs/advanced#refs

const Input = styled.input`
	padding: 0.5em;
	margin: 0.5em;
	color: palevioletred;
	background: papayawhip;
	border: none;
	border-radius: 3px;
`;

class Toto extends React.Component {
	render() {
		return (
			<input {...this.props} />
		);
	}
}

const Input2 = styled(Toto)`
	padding: 0.5em;
	margin: 0.5em;
	color: palevioletred;
	background: red;
	border: none;
	border-radius: 3px;
`;

const Form = () => (
	<Input
		placeholder="Hover here..."
		innerRef={x => this.input = x}
		onMouseEnter={() => this.input.focus()}
	/>
);

const Form2 = () => {
	<Input2	placeholder="Hover here..."
		innerRef={x => this.input2 = x}
		onMouseEnter={() => this.input2.focus()}
	/>
}

render(
	<div>
		<Form />
		<Form2 />		
  	</div>
);

I would like to see a working demo of the innerRef using my demo. I wasn’t able to produce it due to JS errors :

 VM802:29 Uncaught TypeError: Cannot read property 'focus' of null

This seems to happen when I edit too much the javascript in the page.

Could it possible to see both example working ?

Issue Analytics

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

github_iconTop GitHub Comments

8reactions
joetideecommented, Sep 6, 2017

I am having a similar issue. I have a Button styled component:

<Button innerRef={(elem) => this.btn = elem} />

but I am getting null returned when doing this:

console.log(this.btn);

If I use ‘ref’ instead of ‘innerRef’, then I get the StyledComponent object back. Are you able to advise?

Gist: https://gist.github.com/joetidee/5316feb0a39b52bf9b48399888258781

Read more comments on GitHub >

github_iconTop Results From Across the Web

Advanced Usage - styled-components
This example renders our above themed Button and a second one that uses a second ThemeProvider to invert the background and foreground colors....
Read more >
reactjs - Styled Components v4 refObject - Stack Overflow
I am following how the docs from styled-components have explained to ... I have even tried to use the forwardRef as various examples...
Read more >
masking inputs and more ref fun | /*code-comments*/
From the docs: For example, to use with styled-components, which requires an innerRef: <MaskedInput mask={['(', /[1-9]/, /\d/, /\d/, ...
Read more >
Styled Components - Emotion
Styling elements and components. styled is very similar to css except you call it with an html tag or React component and then...
Read more >
Advanced Guides - Glamorous
You can accomplish this with the innerRef prop. ... With CSS in JS, the goal is to style components and reuse those components....
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