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.

Switch Stateless function cannot be given refs when run unit test

See original GitHub issue

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

Environment: OS: macOS High Sierra 10.13 Node: 8.6.0 Yarn: 1.1.0 npm: 5.3.0 Watchman: 4.9.0 Xcode: Xcode 9.0 Build version 9A235 Android Studio: Not Found

Packages: (wanted => installed) react: 16.0.0-beta.5 => 16.0.0-beta.5 react-native: 0.49.1 => 0.49.1

Target Platform: iOS (11)

Steps to Reproduce

  1. react-native init
  2. Add Switch component to App.js
  3. Run unit test

Expected Behavior

Should run unit test without console.error

Actual Behavior

  console.error node_modules/fbjs/lib/warning.js:33
    Warning: Stateless function components cannot be given refs. Attempts to access this ref will fail.

    Check the render method of `Switch`.
        in Unknown (created by Switch)
        in Switch (created by App)
        in View (created by View)
        in View (created by App)
        in App

Reproducible Demo

App.js file

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import { View, Switch } from 'react-native';


export default class App extends Component<{}> {
  render() {
    return (
      <View>
        <Switch />
      </View>
    );
  }
}

__tests__/App.test.js file

import 'react-native';
import React from 'react';
import App from '../App';

// Note: test renderer must be required after react-native.
import renderer from 'react-test-renderer';

it('renders correctly', () => {
  const tree = renderer.create(
    <App />
  );
});

package.json file

{
	"name": "test",
	"version": "0.0.1",
	"private": true,
	"scripts": {
		"start": "node node_modules/react-native/local-cli/cli.js start",
		"test": "jest"
	},
	"dependencies": {
		"react": "16.0.0-beta.5",
		"react-native": "0.49.2"
	},
	"devDependencies": {
		"babel-jest": "21.2.0",
		"babel-preset-react-native": "4.0.0",
		"jest": "21.2.1",
		"react-test-renderer": "16.0.0-beta.5"
	},
	"jest": {
		"preset": "react-native"
	}
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
sam-vdpcommented, Nov 2, 2017

Some more detail: The issue is caused by the native RCTSwitch component used by Switch.render:

return (
  <RCTSwitch
    {...props}
    ref={(ref) => { this._rctSwitch = ref; }}
    onChange={this._onChange}
  />
);

As you can see this component is assigned a ref. However, react-test-renderer uses the following code to check if a component is stateless:

if (typeof value === 'object' && value !== null && typeof value.render === 'function') {
  // Proceed under the assumption that this is a class instance

Because RCTSwitch doesn’t have a render method, the warning is raised.

See also: https://stackoverflow.com/questions/47058905/using-react-test-renderer-with-switch-causes-stateless-function-components-ca

2reactions
tinaboycecommented, Jan 10, 2018

Bump just to inform I have encountered it today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What does "Stateless function components cannot be given ...
React Redux 3 attaches a ref to the component you give it regardless of whether or not it's stateless. The warning you see...
Read more >
What does "Stateless function components cannot be given ...
React Redux 3 attaches a ref to the component you give it regardless of whether or not it's stateless. The warning you see...
Read more >
Unit Testing in React - Javatpoint
Unit testing is a method of testing that tests the individual software unit in theprocess of isolation. Check the output of a function...
Read more >
React Stateless Functional Components - Best Practices
React provides 3 major APIs for creating re-usable components. In this article we show you why you should prefer stateless functional components over...
Read more >
Testing Recipes - React
Otherwise, tests can become “leaky”, and one test can change the behavior of ... might want to test whether a component renders correctly...
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