点击按钮后,fetch方法被无限调用。

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

After clicking on button fetch method is called endlessly

问题

I want to use a modal within a 'use client' page.tsx. I use this example for this: link. However, if I click on the button, my fetch call is set in an endless loop and called indefinitely, the app hangs. What's the problem here?
I use Next in the 13.4.3 Version.

app/page.tsx:

"use client";

export default async function Home() {
  const [showModal, setShowModal] = useState(false);

  const result = await fetch(`http://localhost:3000/products`, {
    cache: "no-store",
  }).then((res) => res.json());

  <button
    className="bg-pink-500 text-white active:bg-pink-600 font-bold uppercase text-sm px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1 ease-linear transition-all duration-150"
    type="button"
    onClick={() => setShowModal(true)}
  >
    Open regular modal
  </button>;
}

After clicking on the button:

点击按钮后,fetch方法被无限调用。

英文:

I want to use a modal within a 'use client' page.tsx. I use this example for this: https://www.creative-tim.com/learning-lab/tailwind-starter-kit/documentation/react/modals/regular. However, if I click on the button, my fecth call is set in an endless loop and called indefinitely, the app hangs. What's the problem here?
I use Next in the 13.4.3 Version.

app/page.tsx:

&quot;use client&quot;;

export default async function Home() {
const [showModal, setShowModal] = useState(false);

const result = await fetch(`http://localhost:3000/products`, {
 cache: &quot;no-store&quot;,
}).then((res) =&gt; res.json());


  &lt;button
      className=&quot;bg-pink-500 text-white active:bg-pink-600 font-bold uppercase text-sm px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1 ease-linear transition-all duration-150&quot;
      type=&quot;button&quot;
      onClick={() =&gt; setShowModal(true)}&gt;
      Open regular modal
  &lt;/button&gt;
...

}

after click on button:

点击按钮后,fetch方法被无限调用。

答案1

得分: 0

你在每次组件渲染时都进行了一次AJAX请求。这可能不是你想要的。

相反,使用useEffect第一次渲染时进行AJAX请求(或者在给定一组依赖项发生变化时进行请求,但在这里似乎不是这种情况),然后用结果更新状态。例如:

export default function Home() {
  const [showModal, setShowModal] = useState(false);
  const [result, setResult] = useState();

  useEffect(function () {
    (async function() {
      const res = await fetch(`http://localhost:3000/products`, {
        cache: "no-store",
      });
      const json = await res.json();
      setResult(json);
    })();
  }, []);

  // 组件的其余部分...
}
英文:

You're making an AJAX request on every render of the component. That's probably not what you want.

Instead, use useEffect to make the AJAX request on the first render (or any time a given set of dependencies changes, which doesn't appear to be the case here) and update state with the result. For example:

export default function Home() {
  const [showModal, setShowModal] = useState(false);
  const [result, setResult] = useState();

  useEffect(function () {
    (async function() {
      const res = await fetch(`http://localhost:3000/products`, {
        cache: &quot;no-store&quot;,
      });
      const json = await res.json();
      setResult(json);
    })();
  }, []);

  // the rest of the component...

}

huangapple
  • 本文由 发表于 2023年5月23日 01:30:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308644.html
匿名

发表评论

匿名网友

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

确定