AI Workshop: learn to build apps with AI →
JSX Recipes: How to move around blocks of code with React and Tailwind

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


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 turned to Tailwind for 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 since this is a simple presentational component without logic, it was a compromise I could live with.

Lessons in this unit:

0: Introduction
1: How to render HTML in React
2: Conditional rendering in React
3: How to loop inside React JSX
4: React, how to make responsive JSX
5: How to repeat displaying something in JSX
6: ▶︎ How to move around blocks of code with React and Tailwind