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.

[Native] Update StyledNativeComponent's ref logic to match StyledComponent

See original GitHub issue

Version

  • 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kittencommented, May 4, 2017

@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 well

1reaction
dbertellacommented, May 3, 2017

I can confirm that switching:

// Container.js
export default (props: Props) => <Container {...props} />;

to

class C extends React.Component {
  props: Props;
  render() {
    return <Container {...props} />;
  }
}

export default C;

Will solve the problem. Any thoughts about it?

Read more comments on GitHub >

github_iconTop 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 >

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