React-native-code-push: Warning: Stateless function components cannot be given refs

Created on 10 Mar 2017  路  4Comments  路  Source: microsoft/react-native-code-push

Hi, guys!

First of all, thanks for your amazing work with code-push, it's awesome! 馃槈

Description

After finishing codePush setup process, I've started to see a warning like:

Warning: Stateless function components cannot be given refs 
(See ref "rootComponent" in App created by CodePushComponent). 
Attempts to access this ref will fail.

Reproduction

Currently, I have a setup file which is responsible for defining the main component, which is just like:

setup.js

// ...
let App = () => (
  <Provider store={store}>
    <StyleProvider style={theme}>
      <RouterWithRedux scenes={scenes} />
    </StyleProvider>
  </Provider>
);

module.exports = codePush(App);

index.ios.js / index.android.js

import {AppRegistry} from 'react-native';
const App = require('./config/setup');

AppRegistry.registerComponent('MyApp', () => App);

Except for a _ref_ used on a TextInput, defined on a component with state, I do not use any other _refs_. Removing codepush() makes this warning disappear.

Additional Information

  • react-native-code-push version: 1.17.2-beta
  • react-native version: 0.42.0
  • iOS/Android/Windows version: iOS 10.2
  • Does this reproduce on a debug build or release build? Debug
  • Does this reproduce on a simulator, or only on a physical device? Both
waiting-for-clarification

Most helpful comment

Hi @diegocouto, thank you for reporting the issue. Also please sorry for late reply.
The issue is caused by this line of code. But the root cause of the issue is that stateless component is in use. Our demo app extends React.Component: class CodePushDemoApp extends Component { and use it further with codePush decorator: CodePushDemoApp = CodePush(codePushOptions)(CodePushDemoApp);
To fix the issue could you please try the following:
setup.js

class MyRoot extends Component {
    ...
    render() {
        return <div />
    }
}

MyRoot = codePush(MyRoot);

let App = () => (
  <Provider store={store}>
    <MyRoot />
  </Provider>
);

module.exports = App;

I've also found the code sample that uses the same approach.

Please let me know if it helps or if you have any issues.

All 4 comments

Hi @diegocouto, thank you for reporting the issue. Also please sorry for late reply.
The issue is caused by this line of code. But the root cause of the issue is that stateless component is in use. Our demo app extends React.Component: class CodePushDemoApp extends Component { and use it further with codePush decorator: CodePushDemoApp = CodePush(codePushOptions)(CodePushDemoApp);
To fix the issue could you please try the following:
setup.js

class MyRoot extends Component {
    ...
    render() {
        return <div />
    }
}

MyRoot = codePush(MyRoot);

let App = () => (
  <Provider store={store}>
    <MyRoot />
  </Provider>
);

module.exports = App;

I've also found the code sample that uses the same approach.

Please let me know if it helps or if you have any issues.

@diegocouto, just a friendly ping.

Hi, @sergey-akhalkov! Thank you for your detailed response. 馃槃 I'll be working on this tomorrow and, as soon as I finish this and some other changes I'll update this issue, ok?

Thanks again! 馃槈

UPDATE

Yep, following this approach, it was possible to get rid of the warning. Thanks! 馃憤

@diegocouto, great! Closing this, please feel free to reopen if needed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vira-khdr picture vira-khdr  路  3Comments

ninjz picture ninjz  路  4Comments

jaysig picture jaysig  路  3Comments

SudoPlz picture SudoPlz  路  4Comments

panarasi picture panarasi  路  4Comments