Open In App

Explain basic code snippet of JSX

In this article, we are going to learn about the basic code snippets of react JSX. The full form of the word JSX is JavaScript-XML. This is an extension of the syntax of the JavaScript language. JSX allows us to implement the HTML code snippets into React directly with very few changes in the syntax. 

Significance of JSX:

In terms of syntax, it is not mandatory to write JSX code in React. However, the built-in file structure consists of the components that are written in simple JSX code. The developers find it useful as the JSX code shows more useful error and warning messages while working with the UI inside the JavaScript project. The JSX code also looks cleaner and easier to debug than the templates created with ‘React.createElement( ) and React.appendChild()‘ method.



Explanation of JSX Code Snippet:

Steps to Create the React Application And Installing Module:

Step 1: Create a React application using the following command:

npx create-react-app name_of_the_app

Step 2: After creating the react application move to the directory as per your app name using the following command:



cd name_of_the_app

Project Structure:

package.json:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Creating the JSX Element:

Now we are going to create a JSX Element inside the App component of the App.js file. The steps are discussed below.

Example: Now write down the following code in the App.js file.




// The App.js file
import './App.css';
 
function App() {
  let jsxElement=(
    <div className="App">
      <h1 className="heading">
          Welcome To GeeksforGeeks
       </h1>
      <p>Hello World</p>
    </div>
  );
  return jsxElement;
}
 
export default App;




/* App.css File */
.App {
    margin-left: 100px;
}
 
.heading {
    color: #308d46;
}

Step to Run Application: Run the application using the following command from the root directory of the project.

npm start

Output:


Article Tags :