testing-library / native-testing-library

🐳 Simple and complete React Native testing utilities that encourage good testing practices.
https://native-testing-library.com
MIT License
515 stars 44 forks source link

Invalid hook call. Hooks can only be called inside of the body of a function component #102

Closed lucasluca closed 4 years ago

lucasluca commented 4 years ago

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<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;

lucasluca commented 4 years ago

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