简体   繁体   English

在本机反应中获取无效的钩子调用

[英]Getting invalid hook call in react native

ExceptionsManager.js:179 Error: Invalid hook call. ExceptionsManager.js:179 错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的内部调用。 This could happen for one of the following reasons:这可能由于以下原因之一而发生:

  1. You might have mismatching versions of React and the renderer (such as React DOM)您可能有不匹配的 React 版本和渲染器(例如 React DOM)
  2. You might be breaking the Rules of Hooks你可能违反了 Hooks 规则
  3. You might have more than one copy of React in the same app你可能在同一个应用程序中有多个 React 副本

Getting this error in Playbutton component在 Playbutton 组件中出现此错误

Playbutton.js播放按钮.js

import { useNavigation } from "@react-navigation/native";
import React, { useEffect, useState, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
  View,
  Button,
  StyleSheet,
  Image,
  TouchableOpacity,
  Dimensions,
  NativeModules,
} from "react-native";

var deviceHeight = Dimensions.get("window").height;
var deviceWidth = Dimensions.get("window").width;
import useAppleFunction from "../CustomHooks/useAppleFunction";

const PlayButton = () => {
   
  useEffect(() => {
      useAppleFunction();
  }, []);

}

and here is the custom hook useApplefunction.js这是自定义挂钩 useApplefunction.js

import { StyleSheet, Text, View } from "react-native";
import React, { useEffect } from "react";
import { useSelector } from "react-redux";

const useAppleFunction = () => {
  const Playlist = useSelector((state) => state);
  console.log("reduxcheck=",Playlist);

  useEffect(() => {
    runtimer();
  }, []);

  const runtimer = () => {
     console.log("reduxcheck=1",Playlist);
  };
};

export default useAppleFunction;

const styles = StyleSheet.create({});

React's error messages are very informative and some are written passively aggressive to make you feel bad for not reading enough documentation. React 的错误消息信息量很大,有些是被动攻击性的,让你因为没有阅读足够的文档而感到难过。

The first clue is "Hooks can only be called inside of the body of a function component."第一条线索是“Hooks can only be called inside of a body of a function component”。 , you are calling your custom hook in another hook. ,您正在另一个挂钩中调用您的自定义挂钩。
Also, out of the 3 reasons listed, only one seems to match your case and it's #2, you're breaking the rules of react hooks .此外,在列出的 3 个原因中,只有一个似乎符合您的情况,它是 #2,您违反了react hooks 的规则

So instead of calling your useAppleFunction hook inside useEffect, you should call it at the top level and save the returned value (in your case, nothing) into a variable.因此,与其在 useEffect 中调用 useAppleFunction 挂钩,不如在顶层调用它,并将返回值(在您的情况下为无)保存到变量中。

A hook does not need to return anything, thus if useAppleFunction is supposed to be a hook, then this is fine.钩子不需要返回任何东西,因此如果useAppleFunction应该是一个钩子,那么这很好。 However, this should not be called inside a useEffect .但是,不应在useEffect中调用它。

This is documented here .记录在这里

To avoid confusion, it's not supported to call Hooks in other cases:为避免混淆,其他情况下不支持调用 Hooks:

Do not call Hooks in class components.不要在 class 组件中调用 Hooks。

Do not call in event handlers.不要调用事件处理程序。

Do not call Hooks inside functions passed to useMemo, useReducer, or useEffect.不要在传递给 useMemo、useReducer 或 useEffect 的函数内部调用 Hooks。

Since your hook does not return anything, it is sufficient to just call it at the body of your JSX component outside of the useEffect .由于您的钩子不返回任何内容,因此只需在useEffect之外的 JSX 组件主体调用它就足够了。 If your hook should actually return something, then we can destructure this as usual.如果你的钩子实际上应该返回一些东西,那么我们可以像往常一样解构它。

Remark: There is not need to use var anymore.备注:不再需要使用var了。 It is outdated.它已经过时了。 I have changed it to const .我已将其更改为const

Here is how you could fix it.这是您可以修复它的方法。

Playbutton.js which I assume, should be a JSX component.我假设的Playbutton.js应该是一个 JSX 组件。

import { useNavigation } from "@react-navigation/native";
import React, { useEffect, useState, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
  View,
  Button,
  StyleSheet,
  Image,
  TouchableOpacity,
  Dimensions,
  NativeModules,
} from "react-native";

import useAppleFunction from "../CustomHooks/useAppleFunction";

const deviceHeight = Dimensions.get("window").height;
const deviceWidth = Dimensions.get("window").width;



const PlayButton = () => {
  // since this is a hook, it is not allowed to call it inside a useEffect
  useAppleFunction()
   
  useEffect(() => {
      
  }, []);

  //
}

Yes, the problem is that you are trying to use Dimensions outside the function, and there you got the error.是的,问题是您正尝试在 function 之外使用Dimensions ,然后出现错误。
Rather than that, you should write your logic only after imports .而不是那样,您应该只在imports之后编写您的逻辑。

import { useNavigation } from "@react-navigation/native";
import React, { useEffect, useState, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
  View,
  Button,
  StyleSheet,
  Image,
  TouchableOpacity,
  Dimensions,
  NativeModules,
} from "react-native";
import useAppleFunction from "../CustomHooks/useAppleFunction";

const PlayButton = () => {
var deviceHeight = Dimensions.get("window").height;
var deviceWidth = Dimensions.get("window").width;
   
  useEffect(() => {
      useAppleFunction();
  }, []);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM