Skip to content

How to move around blocks of code with React and Tailwind

New Course Coming Soon:

Get Really Good at Git

While working on a Next.js website I had the need to move a React component to a whole different place in my markup, depending on the size of the screen.

In particular I had a Sidebar component I wanted on the left side of the screen on a big display, but before the content on a smaller display.

Due to the way I organized the HTML markup and the CSS, it was not immediately clear to me how to perform this transition without rewriting a good portion of it.

So I looked at Tailwind to provide me a nice solution.

And this was the way: I added the component twice on the screen, assigning the class hidden xl:block to the “big screen” part, and xl:hidden to the snippet for the smaller screens:

<div className="hidden xl:block">
  <Sidebar />
</div>

...

<div className="xl:hidden">
  <Sidebar />
</div>

The drawback of course is that the component is rendered twice, but being this a simple presentational component without logic, it was a compromise I could live with.

Are you intimidated by Git? Can’t figure out merge vs rebase? Are you afraid of screwing up something any time you have to do something in Git? Do you rely on ChatGPT or random people’s answer on StackOverflow to fix your problems? Your coworkers are tired of explaining Git to you all the time? Git is something we all need to use, but few of us really master it. I created this course to improve your Git (and GitHub) knowledge at a radical level. A course that helps you feel less frustrated with Git. Launching Summer 2024. Join the waiting list!
→ Get my React Beginner's Handbook
→ Read my full React Tutorial on The Valley of Code

Here is how can I help you: