[Native] Update StyledNativeComponent's ref logic to match StyledComponent
See original GitHub issueVersion
- 1.4.4
- react: 16.0.0-alpha.6
- react-native: 0.43.2
Steps to reproduce
- create a styled react native component eg:
const Container = styled.View
- render the component
Expected Behavior
no warnings
Actual Behavior
A warning stating:
Stateless function components cannot be given refs. Attempts to access this ref will fail.
I just upgrade my react-native project from 0.42 to 0.43 and since then I got this warning. I’ve looked into it a little bit and I think it is cause by this line. If I look at the StyleComponent.js
file I can see a check for innerRef
before settings the ref
props (here)
In this commit the check for innerRef
is removed but I’m not sure why, the description only states ‘Fix error’.
To fix this issue I think this change in StyledNativeComponent.js
shoud work.
render() {
const { style, children, innerRef } = this.props // <-- grab innerRef from this.props
const { generatedStyles } = this.state
const propsForElement = { ...this.props }
propsForElement.style = [generatedStyles, style]
if (innerRef) { // <-- check if innerRef is set before setting the ref prop for propsForElement
propsForElement.ref = this.generateRef()
if (isTag(target)) delete propsForElement.innerRef
}
return createElement(target, propsForElement, children)
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Releases - styled-components
Updating styled components is usually as simple as npm install . Only major versions have the potential to introduce breaking changes (noted in...
Read more >How to use styled-components with React Native
In this tutorial, we'll discuss what advantages a library like styled-components has over the general StyleSheet manager in React Native.
Read more >How to use styled components with Material UI in a React app
The styled() method from styled-components takes as input a React component and outputs another React component with a different style. It is ...
Read more >Using Styled Components with React Native - Level Up Coding
Styled Components is a CSS-in-JS library that enables developers to write each component with their own styles and allows the code to be...
Read more >styled-components | Yarn - Package Manager
styled -components ... Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress....
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
@dbertella Can you open a new issue with the minimal code to reproduce this? Something is probably using the
innerRef
prop though, since I don’t see how this could be caused in any other way. However, the version of SC you’re using is a factor as wellI can confirm that switching:
to
Will solve the problem. Any thoughts about it?