简体   繁体   English

React 17:错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用

[英]React 17: Error: Invalid hook call. Hooks can only be called inside of the body of a function component

The use case用例

I am writing a piece of code searching for movies' deatils into the https://themoviedb.org我正在编写一段代码搜索电影的细节到https://themoviedb.org

the code is written in React 17.0.2 using yarn, visual studio code.该代码是使用纱线,Visual Studio 代码在React 17.0.2 中编写的。

The issue问题

I am calling a function, from a useEffect , that will generate a json file from the result of the call to TheMovieDB我从 useEffect 调用function ,这将从调用 TheMovieDB 的结果生成 json 文件

I got the following error, could you help me?我收到以下错误,您能帮帮我吗? **Error: Invalid hook call. **错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.钩子只能在 function 组件的主体内部调用。 ** **

Thank you in advance for the time you will invest on this issue提前感谢您在此问题上投入的时间

useEffect in browser_movie_db.js browser_movie_db.js 中的 useEffect

 useEffect(() => {
    if (slideRows.length > 0 && searchTerm.length > 2) {
      const searchResult = searchInTheMovieDB(category, searchTerm);
      if (searchResult.results.length > 0) {
        setSlideRows(searchResult);
      } else {
        setSlideRows(slides[category]);
      }
    } else {
      setSlideRows(slides[category]);
      console.log("slideRows", slideRows);
    setSlideRows(slides[category]);
  }, [category, searchTerm, slideRows, slides, theMovieDBApikey]);
    }

searchInTheMovieDB function in selection-filter-themoviedb.js在 selection-filter-themoviedb.js 中搜索 InTheMovieDB function

export function searchInTheMovieDB(media_type, query) {
  let result = {};
  if (media_type === "tv") {
    result = {
      tv: [
        {
          title: `${media_type} search result`,
          data: GetSearch(media_type, query),
        },
      ],
    };
  } else {
    result = {
      movie: [
        {
          title: `${media_type} search result`,
          data: GetSearch(media_type, query),
        },
      ],
    };
  }
  return result;
}

GetSearch function in selection-filter-themoviedb.js在 selection-filter-themoviedb.js 中获取搜索 function

export function GetSearch(media_type, query) {
  const [content, setContent] = useState([]);
  useEffect(() => {

    console.log("GetSearch", `${endpoint.Search}/${media_type}?query=${query}`);
    api
      .get(`${endpoint.Search}/${media_type}`, {
        params: {
          api_key,
          query,
        },
      })
      .then((res) => {
        setContent(res.data.results);
      })
      .catch((error) => {
        console.error("error.message", error.message);
      });
  }, [media_type, query]);
  return { [media_type]: content };
}

Error Invalid hook call.错误 无效的挂钩调用。

错误:无效的挂钩调用。 Hooks 只能在函数组件的主体内部调用

the solution解决方案

I have increased the amount of useEffects to identify the process:我增加了 useEffects 的数量来识别过程:

  1. the * searchTerm changes * searchTerm变化
  2. then, the web services is called然后,调用 web 服务
  3. eventually, the slideRows variable is set with a new content最终, slideRows变量被设置了一个新的内容

PS: I have created an async function fetching the data from the webservice PS:我创建了一个异步function 从 web 服务获取数据


    const [searchResults, setSearchResults] = useState([]);

    import axios from "axios";
    const api = axios.create({ baseURL: BASE_URL });

    async function getSearch(media_type, query) {
    api
      .get(`${endpoint.Search}/${media_type}`, {
        params: {
          api_key,
          query,
        },
      })
      .then((res) => {
        setSearchResults(res.data.results);
      })
      .catch((error) => {
        console.error("error.message", error.message);
      });

  }

  useEffect(() => {
    if (searchResults.length > 0) {
      setSlideRows({
        tv: [
          {
            title: `${category} search result`,
            data: searchResults,
          },
        ],
      });
    }
  }, [category, searchResults]);

  useEffect(() => {
    if (slideRows.length > 0 && searchTerm.length > 2) {
      getSearch(category, searchTerm);
    } else {
      setSlideRows(slides[category]);
    }
  }, [searchTerm, slides]);

  useEffect(() => {
    if (searchResults.length > 0 && searchResults.results?.length > 0) {
      setSlideRows(searchResults);
    } else {
      setSlideRows(slides[category]);
    }

    setSlideRows(slides[category]);
  }, [searchResults]);

暂无
暂无

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

相关问题 反应钩子错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React hook Error: Invalid hook call. Hooks can only be called inside of the body of a function component React Native:错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native: Error: Invalid hook call. Hooks can only be called inside of the body of a function component 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 反应本机:.! 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native !!! Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应错误 - 无效的挂钩调用。 钩子只能在 function 组件的内部调用 - React Error - Invalid hook call. Hooks can only be called inside of the body of a function component 反应,得到错误:无效的钩子调用。 Hooks 只能在函数组件的主体内部调用 - React, getting Error: Invalid hook call. Hooks can only be called inside of the body of a function component React - 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用 - React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应原生。 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用 - React Native. Error: Invalid hook call. Hooks can only be called inside of the body of a function component 反应:未捕获的错误:无效的挂钩调用。 钩子只能在 function 组件的内部调用 - React: Uncaught Error: Invalid hook call. Hooks can only be called inside of the body of a function component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM