30-Day React Learning Journey part-2 ( JSX )
Tags: react, webdev, beginners, tutorial
Published: 2022-11-18 17:39:18


## Introduction:

This article is the second part of “[30-Day React Learning Journey (part 1)](https://dev.to/ymhaah/30-day-react-learning-journey-part-1-14od)” Where we talked in the previous part about:  

- Suggestions for learning resources.
- Why you should learn React.
- how to get started.
- wrote our first line in React.

All this with sharing my personal learning experience and some tips + a simplified explanation of what I learned.

We will continue from day 5 to day 7, I know that this seems a little, but these are the basics of React that you must master before we move on to another part.




---

## Links

- I post daily on Twitter about my experience with learning React and other things. Follow me there → **[Youssef Hafnawy](https://twitter.com/hafanwi).**

- All the code shown in this article and more can be found here → [GitHub: 30-Day-React-Learning-Journey](https://github.com/ymhaah/30-Day-React-Learning-Journey)


<aside>
📚 I know you're lazy so I collected all my tweets about React on → [React-Tweet](https://iodized-crawdad-abc.notion.site/React-Tweets-631ddc8f9b6e467e996161026bbb0acc)


![react tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7b7lj0kn1bda2khycvqh.jpeg)

</aside>


<aside>
📚 I have also compiled all the explanations on one page for you → [React-Summary](https://iodized-crawdad-abc.notion.site/React-summary-065b74152727409b8197b656226bf041)

![React summary](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4a39lkv9g3ukgd2hv69x.jpeg)

</aside>


## Notes:

- Remember, this is a learning journey, It is possible that I made mistakes, so if you notice something wrong, tell me and I will correct it.

- I will build this article on the fact that you read the previous one, so if you find me talking about something in little detail, I may have talked about it in the previous part.

- There is no such thing as just two notes, so did you know that React is the second most-starred library on GitHub after Vue?



Without further ado, let's get started with → 30-Day React Learning Journey part-2 ( JSX )

## day 5 ( JS vs JSX )

![https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hod30c1zztignbno3mae.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hod30c1zztignbno3mae.jpg)

*to Create and add an element to the DOM using just JS You need to:*

1. *select the parent element.*
2. *create the element you want.*
3. *Add the contents of the element.*
4. *append the element to the parent.*

```jsx
let father = document.querySelector("#app");
let h1 = document.createElement("h1");
h1.append("Hallo, react");
father.append(h1);

```

Four steps in order to create one element, and this is without adding a class or anything else. Imagine yourself trying to create a whole page in this way. Each element has at least four lines of code.

*with React you can just use JSX! with the main root we create*

```jsx
root.render(<h1>Hallo, react</h1>);

```

**remember it from the first part!**

React took the logical path of creating an element and reduced all these steps using [JSX](https://reactjs.org/docs/introducing-jsx.html) where all you have to do is just write the element!

Yes, HTML directly inside JS. Can you imagine, before I continue, I want you to imagine how it can benefit you? The more you deal with JS before, the more excited you will be about the idea.

I strongly advise you to continue learning JSX using the fact that you can write something HTML-like directly inside JS and try writing to see what works and what doesn't.

Since it is not 100% HTML, so you can go to the documentation and see the differences, or you can experiment and discover it yourself, For me, I discovered about 70% of the differences on my own and will not forget them.

I am interested to know what you discovered and what was your feeling the first time you saw JSX.

and also I still haven't settled on the appropriate way to write this series as I don't know whether to explain to you what I learned or just share the experience and some tricks.

So for now, I will briefly explain to you what I learned in my style.

---

## day 6 ( JSX basics )

![https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8x200tb0wdog5ifbt3p.jpeg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8x200tb0wdog5ifbt3p.jpeg)

[React Without JSX](https://reactjs.org/docs/react-without-jsx.html):

Before we start, I would like to tell you that you can use React without JSX, if you do not want to increase the complexity or size of the work environment in which you work.

And also learning React without JSX shows you how things work under the hood where you will understand the principles and basics of React.

For me, I learned it firsthand, and if I needed something I would go look for it,I have not seen anyone or any course that does not advise you to learn it, and even React advises that

---

[JSX basics](https://reactjs.org/docs/introducing-jsx.html):

In general, React is just JS with a few extra things you call it js++ But you did not modify anything on the basics of the language.

I won't go into detail about how React will know that you are writing JSX, but in general, just write any HTML code and React is smart enough to recognize it.

yes, if you write `<h1>Hallo, react</h1>` Directly inside JS, react will do the four steps and create the element by itself.

Since we are still inside JS we can simply do the following:

- link a JSX element to a variable:

```jsx
let myHeading = <h1>Hallo, react</h1>
console.log(myHeading);

```

- using it with a function or if:

![https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkikzvhqbd9pu6h3ro1v.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkikzvhqbd9pu6h3ro1v.jpg)

```jsx
if ( reactISGood ) {
   let myHeading = <h1>Hallo, react</h1>
} else {
   let myHeading = <h1>Hallo, vue</h1>
}

```

- butting JSX inside an array or an object

```jsx
 let myHeading = [<h1>Hallo, react</h1>, <h1>Hallo, vue</h1> ]

```

why do you think, as I told you that you are inside JS? Use JSX wherever you want with any js feature.

But since we are inside JS we have to follow the rules of JS like:

- the word **"class"** is already taken so we need to use "className" instead

```jsx
let myHeading = <h1 className="veryBigHeading">Hallo, react</h1>

```

- Forget anything like **"aria-label"** it is now called "ariaLabel" yes, everything now is **[camelCase](https://en.wikipedia.org/wiki/Camel_case)**

Yes, that is why I advised you to try yourself and try to discover what works and what does not work and to think as a programmer and try to discover the reason with the least available information, as you will not forget anything you learned in this way.

![https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m130pdiulbgozh5hoe97.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m130pdiulbgozh5hoe97.jpg)

---

## day 7 ( JSX with JS ):

Before we start, let me tell you some tricks that make you read an article that contains personal experience, not just educational content.

![https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v37q4xpyamdgel0lv0e9.jpg](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v37q4xpyamdgel0lv0e9.jpg)

- if you use VsCode Look for this extension **"Simple React Snippets"** It will make your life easier.
- if want to use [Emmet](https://www.freecodecamp.org/news/write-html-css-faster-with-emmet-cheat-codes/) with React (JSX) go to this article: [Enable Emmet support for JSX](https://eshwaren.medium.com/enable-emmet-support-for-jsx-in-visual-studio-code-react-f1f5dfe8809c) thank me later. 
- Did you know that it is the male seahorse that lays eggs, not the female?

---

## JSX with JS

![jsx meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iamwxbru72phyv3iqdgd.jpg)

the same way you use JSX inside JS you can use JS inside JSX!

So, to use JS inside JSX You have to tell react that we've dropped out of JSX and returned to JS, so if you don't do this, whatever you write will be treated as text or as a regular element.

To do this, write whatever you want to be a JS code inside {} 

```jsx

let bestFramework = "Svelte"
let myHeading = (<h1 className="veryBigHeading">Hallo, {bestFramework}</h1>)

```

You can open these brackets anywhere in JSX, allowing you to do Many cool things like:

```jsx

let imgSrc = 
"https://i.insider.com/602ee9ced3ad27001837f2ac?width=700";
let link = "https://twitter.com/hafanwi/status/1593162901744803840"

<img src={imgSrc}></img>
<a href={link} ></a>

// Do not go behind these links

```

- you can map on an array with all your image URLs with one line

- you can create a 100 div with a simple for loop


I'll leave the rest to the imagination of the enthusiastic programmer in you to discover how many things you can do just with this.

---
Of course, a JSX element can contain more than one child, but you can not have more than one element on one variable.

```jsx

const element = (
  <div>
    <ul className="bestFramework">
      <li>Vue</li>
      <li>Svelte</li>
      <li>Remix</li>
    </ul>
  </div>
);

```


![best Framework meme](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaxa7o7l9fu800cjd11d.jpg)

## Conclusion:

I hope you will forgive me for not completing the experience for some of you in this article, as I did not yet know what to focus on here.

Since I did not explain everything about JSX as I did not tell you how JSX is just a big object or a trick to writing more than one element without a father and more.

At the same time, I did not talk about the things I found difficult and what I learned from the JSX experience on my own and others.

Therefore, my detailed explanation of all of the above, as well as my full experience with learning, are collected on one page, as I said previously.

- [React-Tweet](https://www.notion.so/React-Tweets-631ddc8f9b6e467e996161026bbb0acc)

- [React-Summary](https://www.notion.so/React-summary-065b74152727409b8197b656226bf041)


In the end, I would like to hear your opinions about the article.

- What was missing?
- Should I add more or fewer details?
- Do I add more explanation or cut it out and only talk about the experience?

## Author

- professional links:
    - [Twitter](https://twitter.com/hafanwi)
    - [LinkedIn](https://www.linkedin.com/in/youssef-hafnawy/)
    - [GitHub](https://github.com/ymhaah)
- Hire me:
    - [UpWork](https://www.upwork.com/services/product/development-it-a-custom-and-responsive-websites-in-wordpress-1577236892828233728?ref=project_share)
    - [Freelancer](https://freelancer.com/u/ymhaah)
- Blog:
    - [HashNode](https://hafnawi.hashnode.dev/)
    - [Dev.to](https://dev.to/ymhaah)
    - [Medium](https://medium.com/@ymhaah250)