ReactJS Tutorial - 8 - JSX

Codevolution
5 Nov 201811:24

Summary

TLDRThis video introduces JSX, a JavaScript extension widely used in React to simplify coding by allowing developers to write XML-like syntax. While JSX is not required, it makes React code cleaner and easier to read. The tutorial demonstrates how JSX translates into JavaScript using React's `createElement` method, showing the differences between coding with and without JSX. Key differences between JSX and HTML, like using `className` instead of `class`, are discussed, along with potential future changes in React. The video emphasizes JSX's ability to streamline complex code.

Takeaways

  • 📜 JSX stands for JavaScript XML and is an extension to JavaScript syntax, used with React for writing XML-like code for components.
  • 🛠 JSX is not mandatory for writing React applications, but it simplifies the process and makes the code more elegant and readable.
  • ⚙ JSX ultimately compiles into regular JavaScript that browsers can understand.
  • 📂 JSX code can be written in a simpler format compared to using React’s `createElement` method directly.
  • đŸ§‘â€đŸ’» React’s `createElement` method takes three parameters: the HTML tag, an object for properties, and the children elements.
  • 🔧 JSX elements are syntactic sugar for calling `React.createElement`, simplifying component creation with a more familiar syntax.
  • 🔠 Some differences exist between JSX and HTML, such as using `className` instead of `class` and `htmlFor` instead of `for` due to JavaScript keyword restrictions.
  • ⚛ Even when JSX is not explicitly seen in the code, React must be imported because JSX is translated into `React.createElement` calls.
  • 🔄 The React team may introduce breaking changes in future versions, including reverting `className` back to `class`.
  • 📅 Developers should stay updated on upcoming React changes by reviewing the latest documentation, especially with React 18 and 19 on the horizon.

Q & A

  • What is JSX?

    -JSX stands for JavaScript XML and is an extension to the JavaScript syntax used with the React library. It allows developers to write XML-like code for elements and components in React.

  • Why is JSX commonly used in React?

    -JSX simplifies the process of writing React components by making the code more elegant and readable. Although JSX is not a necessity, it provides a familiar syntax for developers and ultimately translates to pure JavaScript that browsers understand.

  • How does JSX work behind the scenes?

    -Behind the scenes, JSX is transformed into `React.createElement()` calls. Each JSX element is syntactic sugar for this method, which constructs the virtual DOM nodes that React uses to efficiently update the browser’s DOM.

  • Can React be used without JSX?

    -Yes, React can be used without JSX. Developers can use `React.createElement()` to create elements and components, but JSX makes the code cleaner and easier to write, especially when working with multiple elements.

  • How does the `React.createElement()` method work?

    -The `React.createElement()` method takes three parameters: the HTML tag (e.g., 'div'), an object of properties (can be `null` if there are no additional properties), and the children (contents) of the element.

  • What are some key differences between JSX and regular HTML?

    -In JSX, certain attributes differ from HTML. For instance, `class` is replaced by `className` because 'class' is a reserved keyword in JavaScript. Similarly, `for` becomes `htmlFor`, and properties like `onClick` are camel-cased (e.g., `onClick` instead of `onclick`).

  • Why do we need to import React even if we don't see it explicitly being used?

    -When JSX is used, it is converted into `React.createElement()` calls. These calls rely on the React library, which is why `import React` is necessary in files that use JSX, even if React isn't directly referenced.

  • How does JSX improve the code compared to using `React.createElement()` directly?

    -JSX significantly improves code readability and simplicity. For components with many elements, JSX is less verbose and more intuitive, while directly using `React.createElement()` can make the code cumbersome and harder to maintain.

  • What happens if we need to add attributes to an element in `React.createElement()`?

    -The second parameter of `React.createElement()` is an object of key-value pairs that represents the attributes. For example, to add an ID attribute to a `div`, we would pass `{ id: 'hello' }` as the second argument.

  • What future changes related to JSX are mentioned in the video?

    -The video mentions that the React team is working on breaking changes that may impact JSX. One notable change is replacing `className` with `class`. These changes are targeted for future versions of React (React 18 or 19).

Outlines

00:00

📘 Introduction to JSX and its Importance in React

In this video, we explore JSX, a core concept in React. JSX, which stands for JavaScript XML, extends JavaScript's syntax, allowing developers to write XML-like code for React elements and components. While it's not essential for building React applications, JSX simplifies the process and makes the code more elegant. Underneath, JSX is compiled into pure JavaScript, which browsers can understand. This paragraph introduces JSX and explains its advantages over regular JavaScript, emphasizing how it enhances the developer experience.

05:02

🔧 Understanding JSX vs. React.createElement

This section dives into how JSX works behind the scenes, contrasting it with using the `React.createElement` method. A simple 'Hello Vishwas' component is created using both JSX and the `createElement` function. The paragraph explains that while JSX looks like HTML, it's actually syntactic sugar for calling `React.createElement`. By comparing both methods, it becomes evident that JSX offers a cleaner and more readable approach to creating React elements.

10:03

đŸ› ïž Fixing React.createElement and Adding Attributes

This section continues the comparison of JSX and `React.createElement`. It highlights how `React.createElement` can accept multiple elements as children and how to render nested HTML tags properly. The paragraph also introduces the use of attributes in JSX, such as `id` and `className`. In React, developers need to use `className` instead of `class` because `class` is a reserved keyword in JavaScript. These nuances are discussed in detail, with examples on how to handle them in both JSX and JavaScript.

✹ JSX Advantages and Key Differences from HTML

The video explains the key differences between JSX and HTML, such as using `className` instead of `class` and `htmlFor` instead of `for`, due to JavaScript's reserved keywords. CamelCase naming conventions, like `onClick` and `tabIndex`, are also introduced. The speaker mentions upcoming breaking changes in React, such as potentially reverting `className` back to `class`. The paragraph concludes by urging viewers to stay updated with React's evolving syntax, especially as future versions bring significant changes.

🎬 Conclusion and Updates on React

The video wraps up by emphasizing the simplicity of JSX and its clear advantages over manually calling `React.createElement`. It also provides a look forward, discussing possible upcoming changes in React's syntax, such as changing `className` back to `class`. Viewers are encouraged to stay updated by checking the provided link for more information on these changes. The video concludes with a reminder to subscribe for future updates and tutorials.

Mindmap

Keywords

💡JSX

JSX stands for JavaScript XML and is an extension to JavaScript that allows developers to write XML-like code for React components. It simplifies writing HTML structures in JavaScript, making the code more readable and easier to manage. In the video, JSX is described as not necessary but a preferred method because it enhances code simplicity and elegance.

💡React

React is a JavaScript library for building user interfaces, especially for single-page applications. It allows developers to create reusable components. The video demonstrates how JSX is used within the React framework, showing how React’s createElement function can be used to build components, and explaining that JSX ultimately compiles to React code.

💡JavaScript

JavaScript is a core programming language of the web that allows developers to create dynamic and interactive experiences. The video explains that JSX is an extension to JavaScript, and React components written in JSX are eventually compiled into regular JavaScript, which browsers understand and execute.

💡createElement

createElement is a method provided by React to create elements without JSX. It requires at least three parameters: the type of HTML tag, properties, and children elements. The video contrasts JSX with createElement, showing how JSX simplifies the process by eliminating the need for multiple createElement calls.

💡Component

A component in React is a reusable piece of code that represents part of the user interface. Components can be functional or class-based. In the video, the example of the 'hello' component is created both with and without JSX to show how React components are rendered and how JSX simplifies the code structure.

💡XML

XML (eXtensible Markup Language) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. JSX borrows the XML-like syntax, allowing developers to write HTML-like code directly in JavaScript files, as explained in the video.

💡HTML

HTML (HyperText Markup Language) is the standard language used to create web pages. In the video, JSX is shown as a syntactic extension of JavaScript that resembles HTML, making it easier for developers to write the UI structure directly in their JavaScript code. JSX looks like HTML but is translated into JavaScript.

💡Attributes

Attributes in JSX are properties applied to HTML elements, such as className, id, and others. The video explains that some attributes differ in JSX from regular HTML—for instance, class is written as className because 'class' is a reserved word in JavaScript. Other examples include 'for' becoming 'htmlFor' in JSX.

💡Functional Component

A functional component in React is a JavaScript function that returns JSX. It is a simple and efficient way to create React components. The video demonstrates how to create a functional component called 'hello' using JSX and how it renders in the browser.

💡Props

Props (short for properties) are arguments passed into React components, allowing them to be dynamic and reusable. The video briefly touches on how attributes like id or className can be added as props to React components, and these attributes influence the appearance or behavior of the component in the DOM.

Highlights

JSX is an extension to JavaScript, allowing XML-like syntax for writing React elements and components.

JSX is not necessary for React applications but enhances code simplicity and elegance.

JSX ultimately translates to pure JavaScript that browsers understand.

React components can be created with JSX, which simplifies the code compared to using React.createElement.

JSX allows for a familiar syntax to many developers, making React code more approachable.

The 'create element' method is used in React to create elements without JSX.

JSX elements are syntactic sugar for calling React.createElement.

Attributes in JSX are specified as an object of key-value pairs.

In JSX, 'class' is replaced with 'className' because 'class' is a reserved word in JavaScript.

JSX requires camelCasing for properties, such as 'onClick' and 'tabIndex'.

React's import is necessary for JSX because it translates to React.createElement.

JSX makes code more readable and maintainable, especially in complex components.

The React team is considering changes to JSX, such as allowing 'class' instead of 'className'.

React 18 or 19 may introduce breaking changes that affect how JSX is written.

It's important to stay updated with the latest changes in React for effective development.

The video provides a practical example of creating a React component with and without JSX.

Transcripts

play00:00

alright guys in this video we are going

play00:03

to learn all about JSX JSX

play00:07

is probably the word you're going to see

play00:09

and hear a lot in the world of react so

play00:12

it's really important that you

play00:14

understand what SGS X and white is

play00:17

abused alright let's begin JavaScript

play00:21

XML or JSON is an extension to the

play00:25

JavaScript language syntax with the

play00:29

react library it's an extension to write

play00:32

XML like code for elements and

play00:34

components and just like XML GSX tags

play00:39

have a tag name attributes and children

play00:43

now why do we need JSX well the truth is

play00:47

JSX is not a necessity to write react

play00:51

applications you can definitely use

play00:54

react without JSX

play00:56

but JSX makes your react code simpler

play00:59

and elegant as you might have already

play01:03

seen it provides a syntax which is

play01:06

familiar to many developers JSX

play01:09

ultimately translates to pure JavaScript

play01:12

which is understood by the browser's so

play01:15

we talked about what is JSX and why do

play01:18

we use it but how does it work behind

play01:20

the scenes what does the code look like

play01:23

without JSX let's take a look at that

play01:27

what I will do is create a react

play01:30

component using JSX

play01:32

and without using JSX that way you not

play01:37

only understand how JSX translates to

play01:39

regular javascript but also appreciate

play01:42

how it brings out simplicity in your

play01:45

code now I'm going to start by creating

play01:48

a new file called hello dot J's within

play01:52

the components folder

play01:59

this is going to be a simple functional

play02:02

component that renders hello vishwas in

play02:05

the browser so first step as always

play02:08

import react

play02:11

second step create the function that

play02:14

returns what appears to be HTML but is

play02:17

in fact JSX Const hello is equal to an

play02:22

arrow function so empty parentheses and

play02:25

within the curly braces we are going to

play02:28

return HTML which is in fact JSX

play02:32

hello vishwas so a div tag within the

play02:37

div tag and h1 tag and the text hello

play02:40

vishwas finally make sure to export it

play02:43

as the default export

play02:49

now in AB dodges I can import the

play02:52

component and include the tag in the

play02:54

render method so import hello from

play02:58

components slash hello I'm going to

play03:01

comment out greet and welcome

play03:03

and instead include the hallo tag

play03:09

if you save the files and take a look at

play03:12

the browser you should be able to see

play03:14

hello which was so this is the JSX

play03:18

version of hello component now let's

play03:22

rewrite the component without using JSX

play03:27

to help us do that the react library

play03:30

provides a create element method so

play03:34

within the function the code now changes

play03:37

to return react dot create element and

play03:42

this method at minimum accepts three

play03:46

parameters the first parameter is a

play03:51

string which specifies the HTML tag to

play03:54

be rendered for our example we need a

play03:57

div tag to be rendered so react dot

play04:00

create element with the first parameter

play04:02

a string called dev

play04:04

the second parameter we get to pass any

play04:07

optional properties for the example we

play04:10

have right now we don't need any

play04:12

additional properties so we can simply

play04:15

pass in a value of null

play04:18

the third parameter is the children for

play04:21

the HTML element that is children for

play04:25

the div tag again for our example we

play04:28

simply have the text hello which was

play04:30

which we will pass as the third

play04:32

parameter if we now take a look at the

play04:39

browser you should be able to see the

play04:41

text hello which was but this is

play04:45

slightly different from what we had

play04:47

before the h1 tag is missing

play04:50

so let's include that each one and a

play04:55

closing h1 if you now take a look at the

play04:58

browser the output is not what we were

play05:01

looking for if we inspect the element

play05:05

you can notice that it is just inner

play05:08

text - the div tag and the h1 tag is not

play05:11

a Dom node let's fix this if we go back

play05:15

to vs code it turns out that the create

play05:18

element method can accept any number of

play05:21

elements as children so let's split the

play05:25

h1 tag and the text into two elements so

play05:29

the third parameter is going to be now

play05:32

the h1 tag and the fourth parameter is

play05:35

going to be the text hello vishwas

play05:41

if you now take a look at the browser

play05:43

the output is slightly different but it

play05:47

is still not what we are expecting it to

play05:49

be if I inspect the element now we still

play05:53

have the div element but it contains two

play05:56

text nodes each one is a text node and

play05:59

hello vishwas is a text node to render

play06:04

each one as an h1 element and not as a

play06:07

text node what we have to do is call the

play06:10

create element method for the second

play06:12

time so if I go back to vs code the

play06:17

third parameter now is going to be again

play06:19

react dot create element the first

play06:23

parameter is the tag so h1 we have no

play06:27

additional properties so null and

play06:30

finally the text hello

play06:32

vishwas if you save this and take a look

play06:36

at the browser the output is finally

play06:38

what we were looking for

play06:39

hello vishwas an h1 tag now let's go

play06:44

back and take a look at the second

play06:46

parameter we have been ignoring the

play06:49

second parameter basically is an object

play06:52

of key value pairs that will be applied

play06:55

to the element for example let's say we

play06:58

need an ID attribute on this div tag we

play07:02

can specify an object key is ID and the

play07:07

value is hello if you now take a look at

play07:10

the browser and inspect the element you

play07:13

can see the ID attribute with a value of

play07:15

hello similarly let's say we need to add

play07:19

a class to the div tag so within the

play07:22

object class of value dummy class if you

play07:28

take a look at the browser and inspect

play07:30

the element you can see the class

play07:32

attribute being applied but if you take

play07:36

a look at the console you're going to

play07:38

find a warning invalid Dom property

play07:42

class did you mean class name

play07:46

in JavaScript class is a reserved word

play07:49

we did see the class keyword being used

play07:53

to create react components in the last

play07:55

video so in react a CSS class has to be

play07:59

specified using the class name attribute

play08:03

so instead of class it has to be class

play08:06

name camel cased take a look at the

play08:09

browser there is no warning in the

play08:11

console and if you inspect the element

play08:14

you can see that we still have the class

play08:16

attribute being applied as dummy class

play08:20

now the same in JSX is going to be do

play08:27

last name is equal to dummy class so

play08:33

there you go a react component with and

play08:36

without using JSX

play08:38

and by now it must be clearly evident

play08:41

which is the more simpler approach

play08:44

basically each JSX element is just

play08:47

syntactic sugar for calling react dot

play08:51

create element and that is why you have

play08:54

to import the react library when you use

play08:57

JSX

play08:59

if you take a look at grete Jess you

play09:03

probably wonder why is react being

play09:06

imported I don't even notice it being

play09:09

used anywhere but the fact is JSX

play09:12

translates into react dot create element

play09:15

which in turn uses the react library our

play09:20

example just has two elements but

play09:24

imagine a component with ten or a

play09:26

hundred elements it starts to become

play09:28

really clumsy chair sex on the other

play09:32

hand will keep the code elegant simple

play09:35

and readable let me also talk to you

play09:38

about some of the differences you are

play09:40

going to see in JSX

play09:41

compared to regular HTML

play09:45

the first one which we have already seen

play09:48

is class being replaced by class name

play09:52

similarly we also have for being

play09:55

replaced by html4 again for is a keyword

play09:59

in JavaScript you are also going to see

play10:02

kami case property naming convention

play10:05

instead of HTML attribute names for

play10:08

example on click and tab index will

play10:10

become on click and tab index that is

play10:13

camel casing we will see these

play10:17

differences as we progress through the

play10:19

series so don't worry about having to

play10:21

memorize them lastly I want to point out

play10:25

a link which may be very relevant

play10:28

depending on the time you are watching

play10:30

this video the react team is working on

play10:34

making some breaking changes which of

play10:36

course is for the good of react but that

play10:39

also means you might see changes in the

play10:41

code you write one of the biggest

play10:43

changes proposed is changing class name

play10:46

to just class there are a couple more

play10:49

changes listed as well so I will leave a

play10:52

link in the description down below the

play10:55

changes are targeted for react 18 or 19

play10:57

I believe so we have a good year or two

play11:01

before these breaking changes are made

play11:03

now with the less please to take a look

play11:06

at this link as it will help you stay

play11:08

updated with the changes all right

play11:12

that is pretty much what I wanted to

play11:14

discuss about JSX

play11:17

thank you guys for watching please don't

play11:19

forget to subscribe and enable

play11:21

notifications and see you guys in the

play11:22

next video

Rate This
★
★
★
★
★

5.0 / 5 (0 votes)

Étiquettes Connexes
JSXReactJavaScriptWeb DevelopmentComponentsJSX SyntaxProgrammingFrontendReact ElementsCoding Tips
Besoin d'un résumé en anglais ?