Learning React : JSX
Tags: react, javascript, beginners, webdev
Published: 2023-05-25 10:33:01

I am continuing to learn React. It is my second day learning React. If you are only reading this article then you can also visit [my experience of learning React on Day 1](https://dev.to/rabibsust/at-last-i-am-learning-react-2bon). :smile:

Let's hope I will get the knowledge to work with React very soon and just not learn then forget. 😁😅

So, yesterday we stopped after creating a React element using the `React.createElement()` method. 😎
Today we will learn about `JSX-> JavaScript XML`.
`JSX` looks almost like HTML and it is really easy to use while using React.

![JSX and HTML similarity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yihjivczdd1h9ha3rqio.jpg)
So I am creating a similar React component for the increment button that we did in vanilla JS yesterday. Let’s update in the `increment.js` file.
```
const domContainer = document.querySelector('#root');
const reactIncrement = (
    <div>
        <h1 id="display">0</h1>
        <div>
            <button id="button">Increment +</button>
        </div>
    </div>
);

ReactDOM.render(reactIncrement, domContainer);
```
Here as we can see I have simply written JSX to render the element in the root div. Let me run the html now. But I got an error in the console. 😞
```
Uncaught SyntaxError: Unexpected token '<' (at increment.js)
```
The reason for this error is Javascript doesn't recognize the JSX tags we have used. Even though I renamed the `increment.js` file to `increment.jsx (As I am now using JSX :wink:)` it didn't work. :worried:

![Using JSX](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qntlo809dapcacyxykm.jpg)

To get rid of this situation at last we found a way. We need to use a **transpiler**.
**Transpilers** are tools that convert one programming language into another language. :astonished:
In this case of React we will use Babel as a Transpiler. Babel will convert our JSX codes to vanilla JS.
There are also other use cases of Babel to help us coding without worring about the compatibility of different browsers. :heart_eyes:

Here I checked my [JSX code in babel.io](https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&build=&builtIns=false&corejs=3.21&spec=false&loose=false&code_lz=DwEwlgbgfAUABAxSHABYEY5hAXgETgDOADgDYCGAnunlAAzAD0Gsybcokr7PwARgFcALkID2AOyy48gkRJpQAkuIDGAJwCmAWw3ihcANRNZY8dx4pG4aPHZNrUIA&debug=false&forceAllTransforms=false&modules=false&shippedProposals=false&circleciRepo=&evaluate=false&fileSize=false&timeTravel=false&sourceType=module&lineWrap=true&presets=env%2Creact%2Cstage-2&prettier=false&targets=&version=7.21.8&externalPlugins=&assumptions=%7B%7D) website how it converted our JSX code to JS.

So let's go to [babel website -> setup -> In the browser link](https://babeljs.io/setup#installation) and copy the babel script to our html file. Also let's add the increment.js script type to `"text/babel"`.
```
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
    <script type="text/babel" src="./increment.jsx"></script>

```
Now when we run the html in the browser we can see that the counter button and display is appearing and there is no console error.

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

- Let's add the increment functionality in the JS file.
Let's first create an Increment method.
```
const Increment = () => {};
```
- Now let's copy the JSX codes we wrote before in the `increment.js` file inside the function, so that this Increment method returns that JSX code.
```
const Increment = () => {
  return (
    <div>
        <h1 id="display">0</h1>
        <div>
            <button id="button">Increment +</button>
        </div>
    </div>
  );
}
// Render Increment
ReactDOM.render(<Increment />, domContainer);
```
We are still viewing the same button and display with no functionality update till now. :unamused:
![Display Button](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdic84hmwl7oove8vod8.png)
Now let's copy the increment functionality from the vanilla JS code that we wrote yesterday in our `increment.js` file.
```
const domContainer = document.querySelector('#root');
const Increment = () => {
  return (
    <div>
        <h1 id="display">0</h1>
        <div>
            <button id="button">Increment +</button>
        </div>
    </div>
  );
}

let number = 0;

let button = document.querySelector('#button');
let display = document.querySelector('#display');

button.addEventListener('click', () => {
    number++;
    display.textContent = number;
});

ReactDOM.render(<Increment />, domContainer);
```
Did we get what we wanted? No! :disappointed:
I got a console error after copying this.

![Error in Console](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvw5b246tl9enyy6ldg9.png)
Let's fix this issue now.

![Button null](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/236ck0q2vnac8mojxmdi.png)

So from the console error we found that the button is null. That means the button is not defined. So it seems we are calling the button event even before the button is initialized and rendered by calling the `Increment` function.

So now let's render the elements first before calling the button.
```
ReactDOM.render(<Increment />, domContainer);

let number = 0;

let button = document.querySelector('#button');
let display = document.querySelector('#display');

button.addEventListener('click', () => {
    number++;
    display.textContent = number;
});
```
It works now!

![Incrementing number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4qicjsn77x1wa1f087b.gif)
But does it solve the problem? Can we use multiple components this way? Let's say we want two counters. Will it work? This is the reason we are working with React right?
The answer is no. :sob: We still haven't achieved what we want.

I tried adding the Increment component multiple times in the render. 
```
ReactDOM.render(<><Increment /><Increment /><Increment /></>, domContainer);
```
The functionality should work independently, but it didn't.

![All component not working](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6t4vnkb5qlr08jhcfla.gif)
In the above image we saw that only the first component is working but the others aren't.

The main reason for the above issue is that we aren't doing things in React way. Now let's do our coding in the React way. :muscle:

React tells us that we shouldn't manipulate DOM. But in our code we are still selecting DOM components, creating `counter` variable then updating `counter` using DOM manipulation. Which is actually prohibited in React.

So let's first remove our button and display counter functionality code.

To achieve this functionality using React we need to use **State**.

> State is a built-in React object which is used to contain data or information about the component.

So in our counter functionality what is the state? This is our `number` variable. Because only this data is changing and based on this our components' content is changing.

Now let's use this knowledge to create a State inside the Increment function.
```
const Increment = () => {
    const counter = React.useState(0);
    ...
};
```
So what did I do there? I am using `useState` and setting an initial value which is `0` in this case. Let's do a `console.log(counter)`.

![React State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zt8dn7gyd8jfo52iu09s.png)
So, it returns an array which has two elements. 
The first one is the value we set for the state and the second one is a function.

This function actually helps us by returning an updated value based on the parameter we will pass to the function.

So, this function updates the state and triggers a re-render of our component.
This can be used like this:

```
const [state, setState] = React.useState(initialValue);
```
Let's update our `Increment` method now.
```
const [counter, setCounter] = React.useState(0);
```
Here we created the `setCounter` function which we will call during the button click event. So our updated button will be:
```
<button id="button" onClick={ () => setCounter(counter + 1) }>Increment +</button>
```
I am using JSX. The `onClick` function isn't the same as normal HTML. Couldn't find the issue the first time. Also I am using curly braces `{}` not quotation `"` like we used to use in HTML. Found these differences in JSX for now. I need to get used to it :laughing:

To show that counter in the display we will update our display area where we will bind the `counter` variable inside `h1`.
```
<h1 id="display">{ counter }</h1>
```
So after the updates I have now successfully created a React component, which is reusable and can be used several times. And most interestingly I can call this multiple times and can have multiple independent components without writing more codes. :sweat_smile:

This is my JS code now.
```
const Increment = () => {
    const [counter, setCounter] = React.useState(0);
    return (
        <div>
            <h1 id="display">{ counter }</h1>
            <div>
                <button id="button" onClick={ () => setCounter(counter + 1) }>Increment +</button>
            </div>
        </div>
    );
};

ReactDOM.render(<div className="container"><Increment /><Increment /><Increment /></div>, domContainer);

```
I have called the Increment method 3 times and it is now working fine!

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

Here is the [github link](https://github.com/rabibsust/react/tree/master/Day1) for the two projects I have worked till now.

This is all for today. I will try to learn about React DOM tomorrow. Hopefully write something from that. Till then bye! :wave: