简体   繁体   English

未捕获(承诺中)错误:渲染的钩子比上一次渲染期间更多

[英]Uncaught (in promise) Error: Rendered more hooks than during the previous render

enter image description here I'm using useEffect hook to use fetch api but it doesn't work.在此处输入图像描述我正在使用 useEffect 挂钩来使用 fetch api,但它不起作用。 when there is only one api it is working fine but when i use another api to fetch data using the useEffect hook in the createData function it gives error.当只有一个 api 时它工作正常,但是当我使用另一个 api 使用 createData 函数中的 useEffect 挂钩获取数据时,它会出错。

I did some research and i think it is because of some issues caused in re rendering of component in react, i tried to search for the fix but couldn't find it so I'm posting it, if there is any question kindly ask me in comments I'll give more details about it.我做了一些研究,我认为这是因为在 React 中重新渲染组件时引起了一些问题,我试图搜索修复但找不到它所以我发布它,如果有任何问题请问我在评论中,我将提供有关它的更多详细信息。

export default function Unpaid({ transporterId, getFn }) {
  const [itemData, setItemData] = useState([]);
  const [resData, setResData] = useState([]);


  const idUrl =
    "https://url...";

  useEffect(() => {
    let mounted = true;

    fetch(idUrl)
      .then((data) => data.json())
      .then((data) => setResData(data));

    return () => (mounted = false);
  }, []);

  console.log(resData, "response data");

  const dispatchId = resData.map((item) => item.id);
  console.log(dispatchId, "dispatch id");

  function createData(
    po,
    id
  ) {
    useEffect(() => {
      fetch(
        "https://url+id"
      )
        .then((data) => data.json())
        .then((data) => setItemData(data));
    }, []);

    console.log(itemData, "yohohoho");

    return {
      po,
    };
  }

  function Row(props) {
    const { row } = props;
    const [open, setOpen] = React.useState(false);

    return (
     <>
       jsx content
     </>
    );
  }


  const rows = resData.map((item) =>
    createData(
      item.purchase_order_details.po_number &&
        item.purchase_order_details.po_number.length > 0
        ? item.purchase_order_details.po_number
        : "NA",
     
      item.id
    )
  );
 
  return (
   <>
     jsx content
   </>
  );
}

By calling createData inside resData.map(...) , you are calling useEffect in a loop, which violates the rules for hooks .通过在resData.map(...)中调用createData ,您是在循环中调用useEffect ,这违反了hooks 的规则 You can call hooks only on the top level.您只能在顶层调用钩子。

To fix this, you should move the effect outside of createData to the top level, add the dependency resData to the and loop over resData inside the effect.要解决此问题,您应该将createData之外的效果移到顶层,将依赖resData添加到效果内并在 resData 上循环。

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

相关问题 Uncaught Invariant Violation:渲染的钩子比上一次渲染时更多 - Uncaught Invariant Violation: Rendered more hooks than during the previous render 反应钩子错误:渲染的钩子比上一次渲染时更多 - React hooks error: Rendered more hooks than during the previous render 错误:渲染的钩子比上一次渲染时更多 - Error: Rendered more hooks than during the previous render 错误:在 React Native 中渲染的钩子比上一次渲染时多 - Error: Rendered more hooks than during the previous render in react native React 中的错误:渲染的钩子比上一次渲染时更多 - Error in React: Rendered more hooks than during the previous render 错误:渲染了比上一次渲染更多的钩子,handleRemove 导致渲染了更多的钩子错误 - Error: Rendered more hooks than during the previous render, handleRemove causing rendered more hooks error React Hooks 渲染了比上一次渲染更多的钩子 - React Hooks Rendered more hooks than during the previous render React native - “渲染的钩子比之前的渲染更多?” - React native - "Rendered more hooks than during the previous render?" Next.JS “渲染的钩子比上一次渲染时更多” - Next.JS “Rendered more hooks than during the previous render” 在 apollo useQuery 之后渲染的钩子比上一次渲染时更多 - Rendered more hooks than during the previous render after apollo useQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM