This website collects cookies to deliver better user experience
Simplify condition rendering in React.js
Simplify condition rendering in React.js
As a professional developer you are forced to stay up to date with the latest trends in technology. This year I've added Svelte to my bucket list as a new framework to learn.
While researching Svelte I was surprised by the way they handle condition rendering.
Take a look at this example found in their docs:
{#if user.loggedIn}<button on:click={toggle}>Log out
</button>{/if}
Everything is neatly wrapped with if clause and separated from normal flow.
After a quick prototyping I present to you the most powerful component I have ever written, an IF component.
constIF=({ condition, children })=>{if(!condition)returnnull;return<>{children}</>;};
By offloading condition into a separate component this will improve code cleanness and readability by quite a margin (for free).
Let’s imagine the Svelte example in our React app. It would go something like this: