Avoiding rendering children twice
Answered
Red-shouldered Hawk posted this in #help-forum
Red-shouldered HawkOP
Heya!
I have a component that's rendering a different layout, depending on the width of the screen.
Though, I have a small issue. It's not the end of the world, but I'd like to avoid it regardless:
When changing the width of the screen, I return a different layout, and thus, the children prop is being re-rendered.
I've been trying to find a solution to avoid this re-render, since it's also fetching some data, which is kinda slow.
If anyone has any idea of how I could do that, it would be great.
This is my current code:
So as you can see, I can't use conditional rendering to have
Thanks again! â¤ï¸
I have a component that's rendering a different layout, depending on the width of the screen.
Though, I have a small issue. It's not the end of the world, but I'd like to avoid it regardless:
When changing the width of the screen, I return a different layout, and thus, the children prop is being re-rendered.
I've been trying to find a solution to avoid this re-render, since it's also fetching some data, which is kinda slow.
If anyone has any idea of how I could do that, it would be great.
This is my current code:
// If mobile, we want the layout to be like this:
// Sidebar | Page Title
// Main Content
// Main Content
if(isTabletOrMobile) {
return (
<div className="flex flex-col">
<div className="flex flex-row">
<Sidebar />
<PageTitle title="Add Service Call" />
</div>
<div className="w-screen pl-4 pr-4">
<main className="bg-white rounded shadow p-4 mt-4 w-full" dir="rtl">
{ children }
</main>
</div>
</div>
);
}
// If not mobile, we want the layout to be like this:
// Sidebar | Page Title
// Sidebar | Main Content
// Sidebar | Main Content
return (
<div className="flex flex-row">
<Sidebar />
<div className="flex flex-col w-full mr-4 ml-4">
<div className="mt-4">
<PageTitle title="Add Service Call" />
</div>
<main className="bg-white rounded shadow p-4 mt-4 w-full" dir="rtl">
{ children }
</main>
</div>
</div>
);So as you can see, I can't use conditional rendering to have
{ children } appear only once in my code, resulting in the re-rendering.Thanks again! â¤ï¸
Answered by Red-shouldered Hawk
Solved with the following conditional rendering:
not the prettiest but it works! 😄
return (
<div className={ `flex flex-${ isTabletOrMobile ? "col" : "row" }`}>
{ isTabletOrMobile
? <div className="flex flex-row">
<Sidebar />
<PageTitle title={ title } />
</div>
: <Sidebar />
}
<div className={ isTabletOrMobile ? "w-screen pl-4 pr-4" : "flex flex-col w-full mr-4 ml-4" }>
{ !isTabletOrMobile &&
<div className="mt-4">
<PageTitle title={ title } />
</div>
}
<main className="bg-white rounded shadow p-4 mt-4 w-full" dir="rtl">
{ children }
</main>
</div>
</div>
)not the prettiest but it works! 😄
2 Replies
Red-shouldered HawkOP
Solved with the following conditional rendering:
not the prettiest but it works! 😄
return (
<div className={ `flex flex-${ isTabletOrMobile ? "col" : "row" }`}>
{ isTabletOrMobile
? <div className="flex flex-row">
<Sidebar />
<PageTitle title={ title } />
</div>
: <Sidebar />
}
<div className={ isTabletOrMobile ? "w-screen pl-4 pr-4" : "flex flex-col w-full mr-4 ml-4" }>
{ !isTabletOrMobile &&
<div className="mt-4">
<PageTitle title={ title } />
</div>
}
<main className="bg-white rounded shadow p-4 mt-4 w-full" dir="rtl">
{ children }
</main>
</div>
</div>
)not the prettiest but it works! 😄
Answer
Brown bear
I would suggest to render them both and just hide/show the correct one with CSS 🙂