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:
You might have mismatching versions of React and the renderer (such as React DOM)
You might be breaking the Rules of Hooks
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';
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:
const ModalInputButton: FunctionComponent<ButtonProps & {containerStyle?: StyleProp}> = ({ onPress, title, containerStyle, ...otherProps }) => { const [isVisible, setIsVisible] = useState(true); const formattedTitle = Platform.OS === 'android' ? title.toUpperCase() : title; return (
<Text {...otherProps}>{formattedTitle}
); };
export default ModalInputButton;