React-testing-library: Invalid hook call. Hooks can only be called inside of the body of a function component.

Created on 13 Jan 2020  路  3Comments  路  Source: testing-library/react-testing-library

Versions
$ cat package.json | jq .dependencies+.devDependencies | grep -v @types
{
"react": "^16.8.6",
"react-native": "^0.59.8",
"styled-components": "^4.2.0",
"@babel/cli": "^7.4.4",
"react-native-testing-library": "^1.7.0",
"react-test-renderer": "^16.8.6",
"typescript": "^3.5.0-rc"
}
Description
Trying to test hooks based code. Not sure what I'm doing wrong as hooks look like to be supported in the docs & I've seen PR's about it but adding a simple const [isVisible, setIsVisible] = useState(true); to a working (test passing) component ends up with the error:

Invariant Violation: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://fb.me/react-invalid-hook-call for tips about how to debug and fix this problem.

Reproducible Demo
import React, {useState, FunctionComponent} from 'react';
import {
TouchableOpacity,
ViewStyle,
Platform,
View,
ButtonProps,
TouchableNativeFeedback,
StyleProp,
Text
} from 'react-native';

const ModalInputButton: FunctionComponent}> = ({
onPress,
title,
containerStyle,
...otherProps
}) => {
const [isVisible, setIsVisible] = useState(true);
const formattedTitle = Platform.OS === 'android' ? title.toUpperCase() : title;
return (



);
};

export default ModalInputButton;

Most helpful comment

@lucasluca Do you remember exactly what you did to resolve this? I'm running into the same issue, but simply bumping the react & react-dom versions isn't fixing things.

All 3 comments

Resolved, just update react-dom ,react dependencies and "babel-plugin-macros": "^2.6.1",
"babel-plugin-require-context-hook": "^1.0.0",

@lucasluca Do you remember exactly what you did to resolve this? I'm running into the same issue, but simply bumping the react & react-dom versions isn't fixing things.

If someone else runs into this problem, it's similar to the general react problem where there are multiple instances of React. You can get this if you have node_modules in a few different places where you run your tests from. In my case, I have a monorepo:

packages/
packages/package.json
packages/node_modules/
packages/lib1/
packages/lib1/package.json
packages/lib1/node_modules

I run my tests from packages, but had previously compiled my lib1, leaving packages/lib1/node_modules in place. Deleting packages/lib1/node_modules before running tests against lib1 from the packages directory fixes this. Otherwise, you'll be running the tests, which will resolve React from the packages directory, and calls inside lib1 will resolve React to inside the lib1/node_modules directory.

Was this page helpful?
0 / 5 - 0 ratings