Uncaught Error: Rendered more hooks than during the previous render. – reactjs issue

huangapple go评论67阅读模式
英文:

Uncaught Error: Rendered more hooks than during the previous render. - reactjs issue

问题

我正在尝试使用useSwr获取主要数据,并使用axios调用另一个函数,但这样做会导致出现Uncaught Error: Rendered more hooks than during the previous render错误。我至少尝试了1000次。如果你认为这个问题很愚蠢,请忽略它,因为我是新手。我的代码如下,我正在获取主要数据,并尝试使用主要数据获取另一个函数,即{GetDetailsComponent(i.scrip_cd)},我的朋友出现了错误。

import React, { useState, useEffect } from "react";
import "../index.css";
import useSWR from "swr";
import Popup from "reactjs-popup";
import axios from "axios";

const Winner = () => {
  const [title, setTitle] = useState("");
  const GetDetailsComponent = (scrip_cd) => {
    useEffect(() => {
      axios
        .get(
          `https://bse-api-server.vercel.app/getDetails?scripcode=${scrip_cd}`
        )
        .then((res) => setTitle(res.data.PBpcUC))
        .catch((err) => console.log(err));
    }, [scrip_cd]);

    return title.toString() === "5" ? "hidden" : title.toString();
  };

  const fetcher = (...args) => fetch(...args).then((res) => res.json());

  const { data, error, isLoading } = useSWR(
    "https://bse-api-server.vercel.app/",
    fetcher
  );

  if (error) return <div>failed to load</div>;
  if (isLoading) return <div>loading...</div>;

  return (
    <div>
      <center>
        <div className="">You're filtering only High Volume.</div>
      </center>

      <table className="mt-4 table w-full p-4">
        <thead
          style={{
            background: "linear-gradient(270deg,#20bf55,#01baef)",
            position: "sticky",
            top: 0,
            color: "#fff",
          }}
        >
          <tr>
            {" "}
            <th className={`border border-black`}>S.no</th>
            <th className="border border-black ">Name</th>
            <th className="border border-black ">Company Name</th>
            <th className="border border-black ">LTP</th>
            <th className="border border-black ">Now %</th>
            <th className="border border-black ">Tr.v</th>
            <th className="border border-black ">Z</th>
            <th className="border border-black ">M.C</th>
            <th className="border border-black ">N</th>
            <th className="border border-black ">C.I</th>
            <th className="border border-black ">SC</th>
            <th className="border border-black ">Volume</th>
            <th className="border border-black ">Max U.C</th>
          </tr>
        </thead>
        {data.Table.sort((a, b) => (a.trd_vol < b.trd_vol ? 1 : -1)).map(
          (i, index) => {
            const hello = GetDetailsComponent(i.scrip_cd);

            return (
              <tbody key={index}>
                <tr className={`border border-black`}>
                  <td className="border border-black "> {index + 1}</td>
                  <td className="underline text-blue-500">
                    <a href={i.NSUrl} target="_blank" rel="noopener noreferrer">
                      {i.scripname}
                    </a>
                  </td>
                  <td className="border border-black p-2 ">
                    {i.LONG_NAME.split(" - ")[0]
                      .replaceAll("Ltd", "limited")
                      .replaceAll("LTD", "limited")
                      .replaceAll(".", " ")
                      .replaceAll("-$", " ")
                      .replaceAll("{", "")
                      .replaceAll("}", "")
                      .replaceAll("(", "")
                      .replaceAll(")", "")
                      .replaceAll("&", "and")}
                  </td>
                  <td className="border border-black ">
                    <span className="font-extrabold text-md text-green-700">
                      {i.ltradert.toString().split(".")[0]}
                    </span>
                    <span className=" text-xs">
                      {i.ltradert.toString().split(".")[1] === undefined
                        ? ".00"
                        : "." + i.ltradert.toString().split(".")[1]}
                    </span>
                  </td>
                  <td className="border border-black p-2">{i.change_percent}</td>
                  <td className="border border-black ">
                    <Popup
                      trigger={
                        <button className="bg-purple-500 p-1 rounded-md text-white">
                          {" "}
                          Tr.v
                        </button>
                      }
                      position="right center"
                    >
                      <a
                        href={`https://in.tradingview.com/chart/qmDo3C1P/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">Konidoni</div>
                      </a>{" "}
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/VViXq6AO/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">Balaanand</div>
                      </a>
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/1gQFO9Ge/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">Mvsrcm</div>
                      </a>
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/3QIPa6zQ/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">alaknanda</div>
                      </a>
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/GYpMww4j/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">shivonica9</div>
                      </a>
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/fPuBRHoA/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">vwuxi0j</div>
                      </a>
                      <hr />
                      <a
                        href={`https://in.tradingview.com/chart/Ls1Vc76A/?symbol=BSE%3A${i.scripname}`}
                        target="_blank"
                        rel="noopener noreferrer"
                      >
                        <div className=" text-black">tx

<details>
<summary>英文:</summary>

I&#39;m trying to fetch main data using useSwr and I&#39;m calling another function with axios and when I do that I&#39;m getting Uncaught Error: Rendered more hooks than during the previous render. I tried 1000 times at least. please ignore this question if you think this is stupid, coz I&#39;m newbie. my code: here, i am fetching the main data and i tried to fetch another function using the main data which is `{GetDetailsComponent(i.scrip_cd)}` and my friend error has came


    import React, { useState, useEffect } from &quot;react&quot;;
    import &quot;../index.css&quot;;
    import useSWR from &quot;swr&quot;;
    import Popup from &quot;reactjs-popup&quot;;
    import axios from &quot;axios&quot;;
    
    const Winner = () =&gt; {
      const [title, setTitle] = useState(&quot;&quot;);
      const GetDetailsComponent = (scrip_cd) =&gt; {
        useEffect(() =&gt; {
          axios
            .get(
              `https://bse-api-server.vercel.app/getDetails?scripcode=${scrip_cd}`
            )
            .then((res) =&gt; setTitle(res.data.PBpcUC))
            .catch((err) =&gt; console.log(err));
        }, [scrip_cd]);
    
        return title.toString() === &quot;5&quot; ? &quot;hidden&quot; : title.toString();
      };
    
      const fetcher = (...args) =&gt; fetch(...args).then((res) =&gt; res.json());
    
      const { data, error, isLoading } = useSWR(
        &quot;https://bse-api-server.vercel.app/&quot;,
        fetcher
      );
    
      if (error) return &lt;div&gt;failed to load&lt;/div&gt;;
      if (isLoading) return &lt;div&gt;loading...&lt;/div&gt;;
    
      return (
        &lt;div&gt;
          &lt;center&gt;
            &lt;div className=&quot;&quot;&gt;You&#39;re filtering only High Volume.&lt;/div&gt;
          &lt;/center&gt;
    
          &lt;table className=&quot;mt-4 table w-full p-4&quot;&gt;
            &lt;thead
              style={{
                background: &quot;linear-gradient(270deg,#20bf55,#01baef)&quot;,
                position: &quot;sticky&quot;,
                top: 0,
                color: &quot;#fff&quot;,
              }}
            &gt;
              &lt;tr&gt;
                {&quot; &quot;}
                &lt;th className={`border border-black `}&gt;S.no&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Name&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Company Name&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;LTP&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Now %&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Tr.v&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Z&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;M.C&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;N&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;C.I&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;SC&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Volume&lt;/th&gt;
                &lt;th className=&quot;border border-black &quot;&gt;Max U.C&lt;/th&gt;
              &lt;/tr&gt;
            &lt;/thead&gt;
            {data.Table.sort((a, b) =&gt; (a.trd_vol &lt; b.trd_vol ? 1 : -1)).map(
              (i, index) =&gt; {
                const hello = GetDetailsComponent(i.scrip_cd);
    
                return (
                  &lt;tbody key={index}&gt;
                    &lt;tr className={`border border-black`}&gt;
                      &lt;td className=&quot;border border-black &quot;&gt; {index + 1}&lt;/td&gt;
                      &lt;td className=&quot;underline text-blue-500&quot;&gt;
                        &lt;a href={i.NSUrl} target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot;&gt;
                          {i.scripname}
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 &quot;&gt;
                        {i.LONG_NAME.split(&quot; - &quot;)[0]
                          .replaceAll(&quot;Ltd&quot;, &quot;limited&quot;)
                          .replaceAll(&quot;LTD&quot;, &quot;limited&quot;)
                          .replaceAll(&quot;.&quot;, &quot; &quot;)
                          .replaceAll(&quot;-$&quot;, &quot; &quot;)
                          .replaceAll(&quot;{&quot;, &quot;&quot;)
                          .replaceAll(&quot;}&quot;, &quot;&quot;)
                          .replaceAll(&quot;(&quot;, &quot;&quot;)
                          .replaceAll(&quot;)&quot;, &quot;&quot;)
                          .replaceAll(&quot;&amp;&quot;, &quot;and&quot;)}
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black &quot;&gt;
                        &lt;span className=&quot;font-extrabold text-md text-green-700&quot;&gt;
                          {i.ltradert.toString().split(&quot;.&quot;)[0]}
                        &lt;/span&gt;
                        &lt;span className=&quot; text-xs&quot;&gt;
                          {i.ltradert.toString().split(&quot;.&quot;)[1] === undefined
                            ? &quot;.00&quot;
                            : &quot;.&quot; + i.ltradert.toString().split(&quot;.&quot;)[1]}
                        &lt;/span&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2&quot;&gt;
                        {i.change_percent}
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black &quot;&gt;
                        &lt;Popup
                          trigger={
                            &lt;button className=&quot;bg-purple-500 p-1 rounded-md text-white&quot;&gt;
                              {&quot; &quot;}
                              Tr.v
                            &lt;/button&gt;
                          }
                          position=&quot;right center&quot;
                        &gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/qmDo3C1P/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;Konidoni&lt;/div&gt;
                          &lt;/a&gt;{&quot; &quot;}
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/VViXq6AO/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;Balaanand&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/1gQFO9Ge/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;Mvsrcm&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/3QIPa6zQ/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;alaknanda&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/GYpMww4j/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;shivonica9&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/fPuBRHoA/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;vwuxi0j&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/Ls1Vc76A/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;txqm0ga&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/yenE16ib/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;s3uqog1&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/BxyKps7d/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;ke1a6e9&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/ZhZee2zq/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;hztm13v&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/ZhZee2zq/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;ZuViBDG7&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/r37zOlcQ/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;bwbkdkm&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/yvElZCzr/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;ajutam4&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                          &lt;a
                            href={`https://in.tradingview.com/chart/75frF6LZ/?symbol=BSE%3A${i.scripname}`}
                            target=&quot;_blank&quot;
                            rel=&quot;noopener noreferrer&quot;
                          &gt;
                            &lt;div className=&quot; text-black&quot;&gt;agbifw1&lt;/div&gt;
                          &lt;/a&gt;
                          &lt;hr /&gt;
                        &lt;/Popup&gt;{&quot; &quot;}
                      &lt;/td&gt;{&quot; &quot;}
                      &lt;td className=&quot;border border-black p-2 underline text-blue-500&quot;&gt;
                        {&quot; &quot;}
                        &lt;a
                          href={`https://www.google.com/search?q=zauba+${i.LONG_NAME.split(
                            &quot; - &quot;
                          )[0]
                            .replaceAll(&quot;Ltd&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;LTD&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;.&quot;, &quot; &quot;)
                            .replaceAll(&quot;-$&quot;, &quot; &quot;)
                            .replaceAll(&quot;{&quot;, &quot;&quot;)
                            .replaceAll(&quot;}&quot;, &quot;&quot;)
                            .replaceAll(&quot;(&quot;, &quot;&quot;)
                            .replaceAll(&quot;)&quot;, &quot;&quot;)
                            .replaceAll(&quot;&amp;&quot;, &quot;and&quot;)}`}
                          target=&quot;_blank&quot;
                          rel=&quot;noopener noreferrer&quot;
                        &gt;
                          Z
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 underline text-blue-500&quot;&gt;
                        {&quot; &quot;}
                        &lt;a
                          href={`https://www.google.com/search?q=moneycontrol+${i.LONG_NAME.split(
                            &quot; - &quot;
                          )[0]
                            .replaceAll(&quot;Ltd&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;LTD&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;.&quot;, &quot; &quot;)
                            .replaceAll(&quot;-$&quot;, &quot; &quot;)
                            .replaceAll(&quot;{&quot;, &quot;&quot;)
                            .replaceAll(&quot;}&quot;, &quot;&quot;)
                            .replaceAll(&quot;(&quot;, &quot;&quot;)
                            .replaceAll(&quot;)&quot;, &quot;&quot;)
                            .replaceAll(&quot;&amp;&quot;, &quot;and&quot;)}`}
                          target=&quot;_blank&quot;
                          rel=&quot;noopener noreferrer&quot;
                        &gt;
                          M.C
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 underline text-blue-500&quot;&gt;
                        &lt;a
                          href={`https://advancedastra.onrender.com/${i.LONG_NAME.split(
                            &quot; - &quot;
                          )[0]
                            .replaceAll(&quot;Ltd&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;LTD&quot;, &quot;limited&quot;)
                            .replaceAll(&quot;.&quot;, &quot; &quot;)
                            .replaceAll(&quot;-$&quot;, &quot; &quot;)
                            .replaceAll(&quot;{&quot;, &quot;&quot;)
                            .replaceAll(&quot;}&quot;, &quot;&quot;)
                            .replaceAll(&quot;(&quot;, &quot;&quot;)
                            .replaceAll(&quot;)&quot;, &quot;&quot;)
                            .replaceAll(&quot;&amp;&quot;, &quot;and&quot;)}`}
                          target=&quot;_blank&quot;
                          rel=&quot;noopener noreferrer&quot;
                        &gt;
                          N
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 underline text-blue-500&quot;&gt;
                        &lt;a
                          href={`https://chartink.com/stocks/${i.scrip_cd}.html`}
                          target=&quot;_blank&quot;
                          rel=&quot;noopener noreferrer&quot;
                        &gt;
                          C.I
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 underline text-blue-500&quot;&gt;
                        &lt;a
                          href={`https://www.screener.in/company/${i.scrip_cd}/`}
                          target=&quot;_blank&quot;
                          rel=&quot;noopener noreferrer&quot;
                        &gt;
                          SC
                        &lt;/a&gt;
                      &lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2&quot;&gt;{i.trd_vol}&lt;/td&gt;
                      &lt;td className=&quot;border border-black p-2 font-bold&quot;&gt;
                        {/* &lt;GetDetailsComponent scrip_cd={i.scrip_cd} /&gt; */}
                        {GetDetailsComponent(i.scrip_cd)}
                      &lt;/td&gt;
                    &lt;/tr&gt;
                  &lt;/tbody&gt;
                );
              }
            )}
          &lt;/table&gt;
        &lt;/div&gt;
      );
    };
    
    export default Winner;



</details>


# 答案1
**得分**: 1

不建议在组件内部定义组件除非绝对必要

强烈建议将`GetDetailsComponent`的定义移到`Winner`之外

```lang-js
// 也可以将这部分移到自己的可测试定义中
const getDetails = async (scripcode) =>
  (
    await axios.get("https://bse-api-server.vercel.app/getDetails", {
      params: { scripcode },
    })
  ).data.PBpcUC;

// 注意解构props
const GetDetailsComponent = ({ scrip_cd }) => {
  const [title, setTitle] = useState("");

  useEffect(() => {
    getDetails(scrip_cd).then(setTitle).catch(console.error);
  }, [scrip_cd]);

  return title === "5" ? "hidden" : title;
};

您也不能像普通函数一样调用组件。它们必须用作JSX组件

<GetDetailsComponent scrip_cd={i.scrip_cd} />
英文:

It's not usually a good idea to define components within components unless absolutely necessary.

I would strongly suggest moving the GetDetailsComponent definition out of Winner.

// can move this to its own, testable definition too
const getDetails = async (scripcode) =&gt;
(
await axios.get(&quot;https://bse-api-server.vercel.app/getDetails&quot;, {
params: { scripcode },
})
).data.PBpcUC;
// note the props destructuring
const GetDetailsComponent = ({ scrip_cd }) =&gt; {
const [title, setTitle] = useState(&quot;&quot;);
useEffect(() =&gt; {
getDetails(scrip_cd).then(setTitle).catch(console.error);
}, [scrip_cd]);
return title === &quot;5&quot; ? &quot;hidden&quot; : title;
};

You also cannot call components like a regular function. They must be used as a JSX component

&lt;GetDetailsComponent scrip_cd={i.scrip_cd} /&gt;

huangapple
  • 本文由 发表于 2023年7月17日 11:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定