Switch Stateless function cannot be given refs when run unit test
See original GitHub issueIs 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
- react-native init
- Add Switch component to App.js
- 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:
- Created 6 years ago
- Reactions:7
- Comments:10 (1 by maintainers)
Top 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 >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
Some more detail: The issue is caused by the native RCTSwitch component used by Switch.render:
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:
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
Bump just to inform I have encountered it today.