Core React Concept: JSX

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Search for a command to run...

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
No comments yet. Be the first to comment.
Hi friends ๐, I've been writing tech blog posts for a few years now. If you don't know me yet, English is my second language. My mother tongue is Indonesian, and I'm also surrounded by Dutch every da

I frequently receive messages from enthusiastic contributors who have completed 2 or 3 high-quality pull requests (PRs) in just 1 month. Theyโre proactive, their work is top-tier, and theyโve handled

A few months ago, I shared a story about building an automated open source portfolio using just my smartphone and an AI assistant while on vacation. My main goal was to stop the "spreadsheet struggle"

Recently, I read Bekah Hawrot Weigelโs blog post, "When 'Local' Went Global: The Pandemic Era of International Communities." As someone active in many online communities, I found it a bit sad, but I agree with her points and think they apply to open ...

Hacktoberfest is almost over. How has your contribution journey been so far? Are you confident about contributing to open source projects, or are you still struggling to find your way? If you're new to open source and still feel overwhelmed, that's c...

Hello Fellow Codenewbies ๐,
Say we want to create an element in DOM that has not existed yet and append it to a <div> with an id called root.
Then this is what we would do in vanilla Javascript:
HTML:
<body>
<div id="root"></div>
</body>
Javascript:
const root = document.getElementById('root')
const h1 = document.createElement("h1")
h1.innerHTML = "Hello World"
root.appendChild(h1)
React has a unique syntax called JSX which will benefit us from writing long codes as above.
JSX is a syntax extension to JavaScript.
It allows us to combine UI with Javascript logic in a Javascript file.
ReactDOM.render(<h1>Hello World</h1>, document.getElementById("root"))
We have a <h1> tag inside Javascript's method in the example above.
We tell JSX to "render <h1> in an element with id of root."
โ Notes:
Please read the previous post, if you haven't, to understand how ReactDOM.render() works.
Since JSX combines HTML and Javascript, we need Babel to translate JSX and render the HTML onto the page.
When we use CDN, we need to include Babel's CDN.
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
We need to import the React library to enable Babel when using the create-react-app package.
import React from 'react'
โ Side Note:
It's optional, but we can save files that contain JSX codes as .jsx instead of .js (e.g.: Header.jsx). That way, we are aware that the files contain JSX codes.
It's important to note that we cannot have more than one element next to each other in JSX.
When we have many elements to render, we need to wrap those elements inside an opening and closing tag.
Some developers use fragments (<>...</>), and some use <div>...</div> as a wrapper.
For accessibility purposes, we better use fragments because they won't give extra <div>. Thus, it's less confusing for people who use screen readers.
const element = (
<>
<h1>Hello World</h1>
<p>How are you?</p>
</>
)
ReactDOM.render(element, document.getElementById("root"))
For the readable purpose, it's common practice to wrap a return JSX code in ().
Anything inside the opening and closing wrapper tag are called children property.
JSX is more strict than HTML.
One basic rule of JSX is that every element has to have a closing tag.
Think about HTML tags such as <input>, <img>, <br>, <hr>, and so on.
In JSX, we have to close those tags. Otherwise, we will get syntax errors.
const element = (
<>
<h1>Hello World</h1>
<br />
<p>How are you?</p>
<input type="text" placeholder="Type your message" />
<button>Submit</button>
</>
)
ReactDOM.render(element, document.getElementById("root"))
In the previous example, we hardcoded the heading and the paragraph.
What if we want to be able to change a value dynamically?
Writing Javascript inside {} allows us to run Javascript functions and methods.
We can see it as a separator between Javascript and HTML.
const title = "Random Number"
function randomNum() {
return Math.floor((Math.random() * 10) + 1)
}
const myNum = (
<div>
<h1>{title}</h1>
<h2>My number is: {randomNum()}</h2>
</div>
)
ReactDOM.render(myNum, document.getElementById("root"))
Now, the <h1> will get updated when we change the title. And whenever we refresh the page, there will be a random number generated.
โ Important Notes:
Whatever comes inside {} should be a Javascript expression.
<p>...</p>.<input />.<>...</> or <div>...</div>{} allows us to use Javascript logics in JSX. And the logic inside {} should be a Javascript expression.Thank you for reading!
Last but not least, you can find me on Twitter. Let's connect! ๐