Possible solutions for Node.js dynamic content
1. Displaying content over the element
Pros | Cons |
Simplicity | Requires taking into consideration changes in the height of the top bar |
May cause problems with the z-index property, which will in turn cause elements to overlap each other |
One of the simplest solutions to this challenge. However, in practice, we’re only going to overlap the content rather than replace it.
2. The portal
Pros | Cons |
Sounds solution for rendering elements over other elements | Required rendering outside of the main root of the virtual tree |
Overlays elements rather than actually replaces them | |
Next.js doesn’t support portals by default because of SSR (can be achieved with Next.js extensions or custom implementation) |
Similar to displaying a component with the position: absolute property. An interesting solution, but not viable this time due to the way it works. The portals have to be rendered outside of the main root of the React virtual DOM. In the case of Next.js, it also requires additional logic that could make it work properly.
3. Remaking all views
Pros | Cons |
Consistency | Very time-consuming |
Code clarity | May cause a variety of new bugs |
As the app continues to grow, new problems of similar nature may arise, resulting in returning to square one concerning the problem at hand |
One may consider simply remaking all the views. When presented with new requirements from the client that never we as developers nor the business stakeholders anticipated, it may be a sound idea. From the standpoint of code clarity, it’s possible the best solution. However, its time-consuming nature makes it unacceptable. Investing that much time for such a minor task is out of the question.
4. Dynamic view switching
Pros | Cons |
Highly dynamic | High cost of rebuilding the virtual DOM tree |
High customizability of the solution | |
View management from each place and component |
This solution requires me to take a step back. What does it even mean in this context? Let’s say that we have a complex component structure such as this one:
:quality(75))
complex structure example
I want to now make it so that when the user clicks the Button, the default component generated in the Sidebar is replaced with another component.
Simple, isn’t it? All it takes is to create a simple If statement and pass a callback to the parent of each child. Once you edit about 30 files (it’s a big project, after all!) just to deliver and pass a new prop, it’s all done. It sure was messy, but it worked at last. Unfortunately, this daunting task was just the tip of the iceberg.
What if there were more components such as this? For the sake of the argument, let’s make it 3. Naturally, each and every one of them has to switch the view with a new element. Still quite feasible. I could add a simple state management machine (for code quality’s sake), edit 60 files and it’s done! However, it seems that at this point the one that will have the misfortune to read our code may be quickly in need of some sedatives. Still, it’s another success! But is it really?
What if I didn’t know what view I wanted to render? What if the views were not defined yet by the client? Further actions of this kind could quickly cause half of your team to go on an extended vacation.
This is not the kind of solution I need. I’m looking for something that is more dynamic and reusable. Something that can render practically any component and cause the DOM tree to be rebuilt accordingly with just about any component as a starting point. A solution like this should spare me the trouble of nesting or passing the state or its setter.
Even more solutions?
There are definitely more possible solutions to this problem. However, my goal here is not to analyze them all. Rather, I would like to focus on one that I find the most optimal.
Authors
Maksymilian Zyskowski
Originally from the small lovely town of Ełk, Poland. A lover of books and films. Likes to spend time discussing with friends. Passionate about JavaScript and Frontend Development.

:quality(75))
:quality(75))
:quality(75))
:quality(75))
:quality(75))