Conditional Rendering in React.js every beginner must know.
Tags: react, beginners, tutorial, javascript
Published: 2020-12-04 05:04:42

In this post I will discuss how you can use conditional rendering in your React.js projects.

You can conditionally render jsx elements by using two methods.

1.<code> { condition && jsx elements } </code>

2. <code> { condition ? jsx element : other jsx element } </code>

It may seem trivial at first, but it forms the crux of react development as you will be using this stuff a lot in your projects.

Ok lets learn them.

<strong>1. { condition && jsx elements }</strong>

I use this one when there is no element/data in the screen to display to the user.

For example,

When the user first enters your website and wants to add some data into it, we can use this conditional rendering code to say that "you have no entries" or "you have no data entered" or something like that.

```js

return(
<div>

{ !data && <div> You have entries </div>}

<input type="text" placeholder="Write something" value={input_variable}></input> 
</div> 

```

After the user enters a data, the conditional will not render anything as the condition is now false.

Powerful yet simple concept in React.

You can use this concept in a lot of ways here but, I use this conditional for this purpose.

If you use some different logic, please mention it in the comments. 

<strong> 2.  { condition ? jsx element : other jsx element } </strong>

This one is a more advanced one to grasp but is super useful in many use cases.

This conditional can be treated as a super set of the first one mentioned in this post.( Mathematicians will know this concept very well) 

> The first conditional rendering concept just displays only one jsx element if it is true.

> The second one will help you display either one of the two elements you try to display with a conditional.

If you did not understand any of the above I will explain with an example.

So if you want to render an error message if the user input is wrong and in turn if the user input is correct and want to render the input value, you can use this conditional rendering concept.

```js

<div>
<input type="text" placeholder="Write something" value={input_variable}></input> 

{!error ? <div>The input data the user entered {inputdata}</div> : <div>{error_message}</div>}
</div>

```

When I first started learning this conditional rendering concept in react, It was extremely difficult to understand what is going on.

But consistent reading of many articles about this topic and watching YouTube and Udemy videos made me grasp this conditional rendering stuff and now I know how to use it in my projects.

If you are struggling with it, you can save this post and read it again and again to get hold of it.

And that's how you use conditional rendering in your projects.

If you have any doubts please ask in the comments and I will try to solve them.

### Thank you for reading!!

<strong> <u>Check out my portfolio: </u> [Gautham's portfolio](https://gautham-portfolio.netlify.app/)</strong>

<strong> <u> Check out my blog: </u>[coding-magnified.tech](https://coding-magnified.tech)</strong>

If you want to appreciate my or support me, you can buy me a coffee with the link below,

<a href="https://www.buymeacoffee.com/XJvD4NhAK" target="_blank">
<img width="250px" height="100px"src="https://dev-to-uploads.s3.amazonaws.com/i/xwa28cylrit799s70lrs.png" alt="Buy Me a Coffee"</img>
</a>

### <u>My Other Articles: </u>

- [Universal CSS properties everyone must know](https://dev.to/gautham495/universal-css-properties-everyone-must-use-4kie)

- [Create-react-app](https://dev.to/gautham495/creating-a-new-react-app-with-create-react-app-4e9m)

- [Git for beginners](https://dev.to/gautham495/basic-git-knowledge-for-beginners-m4p)

 
- [Change headers in react with react-helmet](https://dev.to/gautham495/changing-headers-in-react-with-react-helmet-d0d)

- [Know How to apply box-shadow on all four sides.](https://dev.to/gautham495/know-how-to-apply-box-shadow-on-all-four-sides-43pn)
 
- [Simple CSS Selectors](https://dev.to/gautham495/simple-css-selectors-for-beginners-2i44).

- [CSS Pseudo Elements](https://dev.to/gautham495/css-pseudo-elements-for-beginners-53ob).