What is React js and how JSX works in react js

React Basic :

React js is a library and it is used to develop single page applications. React js is a front-end technology and it is completely based on javascript.

To create a react project you need to install node js first.

Download node js from this link https://nodejs.org/en/

After successfully installing node js install react js by these commands, open your terminal and run these commands.

npx createreactapp your_app_name
cd your_app_name
npm start

Now your react app will launch after running npm start command.

What is JSX :

JSX stands for syntax extension to javascript. Its kind of a template language with full power of javascript.

Inside react if we want to show a H1 tag without JSX then we need to write these long syntax.

import React from ‘react’;
import ReactDOM from ‘react-dom’;
import ‘./index.css’;
import App from ‘./App’;
const element = React.createElement(‘h1’,null,“heading text”);
ReactDOM.render(element,document.getElementById(“root”));
// ReactDOM.render(
//   <React.StrictMode>
//     <App />
//   </React.StrictMode>,
//   document.getElementById(‘root’)
// );

Inside index.js file comment out the ReactDOM.render and create a element and render it inside the root id html element.

Output for the above code is

const element = React.createElement(‘h1’,null,“heading text”);

this way JSX works internally to create elements.

JSX format :

For one heading element it is easy to initialize but what happen when you have to write a lot of html code, Its complicated write React.createElement code for all  html code.

To solve this we use JSX which allow you to write all the html code inside a javascript funciton and later this Babel convert automatically all html code to React.createElement javascript code.

Babel is a compiler used in react js for JSX conversion.

Now the same above output you can get with JSX by writing simple html code.

import React from ‘react’;
import ReactDOM from ‘react-dom’;
import ‘./index.css’;
import App from ‘./App’;
ReactDOM.render(
  <h1>Heading text</h1>,
  document.getElementById(‘root’)
);

Now simple html h1 tag can be used inside this render function. This code writing inside the render function is JSX.

lets understand with another file.

create a Demo.js file inside the src folder

const Demo = ()=>{
    return (
        <h1>Demo Data</h1>
    )
}
export default Demo;

This is the code inside the Demo.js file

    return (
        <h1>Demo Data</h1>
    )

This above code represent JSX.

Now we will import this file inside index.js and use it inside the ReactDOM.render.

import Demo from ‘./Demo’
ReactDOM.render(
  <React.StrictMode>
    <Demo />
  </React.StrictMode>,
  document.getElementById(‘root’)
);

This will generate same output with text demo data.

<React.StrictMode>

This helps to show basic javascript errors during compilation.

That’s all for this post, feel free to comment if you have any question.

Related Posts

angular vs react-min

What is the difference between Angular & React JS, Which one you should choose?

Angular & React JS both are use for Frontend Development, To make Single page applications or Web Apps. Here is a quick difference between both of them….

react js icon

What is Redux? What is action, reducer and store in Redux

In this blog we will discus about the redux, what is Redux and how you can use it in React application. What is Redux? Redux is a…

react js icon

What is useEffect Hook? How to Fetch data from API using useEffect

useEffect hook is used to manage side effects that are not related to component rendering. Mostly useEffect hook is used for managing console logs, animations, loading data,…

react js icon

What is useReducer Hook? What is the difference between useState and useReducer Hooks?

useReudcer hook takes the current state and returns a new state, it is used for state management. With useReducer, we can directly call the function inside the…

react js icon

Creating React JS App without using npx create-react-app command

It’s easy to create a react application by just running npx create-react-app project_name command, do you know what are the basic requirements for creating a react application…

react js icon

What is the useState() hooks in React JS | How to use useState() hook in React JS

Hooks are very important in react js and the most common hook is the useState Hook. In this blog, we will look into the functionality of this…

This Post Has One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *