Next.js Discord

Discord Forum

Render error.tsx if error occur inside component directory

Unanswered
Ragdoll posted this in #help-forum
Open in Discord
RagdollOP
I am importing the BlogOption.tsx inside page.tsx for manage route .
 async function fetchBlogs() {
    try {
      const res = await fetch("http://localhost:3001/api/blogs");
      const data = await res.json();
      setBlogs(data);
    } catch (err) {
      throw err;
    }
  }

When the error occur while fetching , the error.tsx should render but its not rendering

1 Reply

Lapland Longspur
fetchBlogs seems like a server action, within it you using setBlogs ? Is set blogs from useState? if so, I dont think you can use state inside here ?


Another thing, which page are you using fetchBlogs in ? the one that is in the same directory as the error.tsx or the page.tsx that is one level above ? if you doing it in one level above, you may need an error.tsx there instead ?