Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
React JSX is the React JavaScript extension, or you can say react JavaScript XML, both are same. So the question is what is React JSX? And why need to know about it?
React’s JavaScript Extension JSX (JavaScript Extension) allows you to write JavaScript code that looks similar to HTML. JSX, an HTML-like syntax that React uses to extend ECMAScript, allows the syntax to co-exist with JavaScript/React. The syntax is preprocessors, i.e., source to source compiler such as babel, to convert HTML-like syntax into a standard JavaScript object that a JavaScript engine can parse.
JSX allows you to create HTML/XML-like structures, such as DOM-like tree structures, in the same file you write JavaScript codes. The preprocessor will then transform these expressions into actual JavaScript Code. JSX tags are similar to XML/HTML in that they have a tag name and attributes.
I know it just looking somewhat confusing, So let’s take an example to understand about it:
import React, { Component } from 'react';
class App extends Component{
render(){
return(
<div>
<h1>Hello World!!!!</h1>
</div>
);
}
}
export default App;
So we have here imported react and component, we need to import these packages to work in react, and we have to extend app class with component, so whatever we write here add to class file as we talk in previous tutorial. Now for render function, this will be used to show the data or whatever you write in this will be show on in the browser. In render function we used div, in this div we added a h1 tag similar to the HTML, so the output we get is:
Looks similar to HTML, right? So we have used 2 HTML tags, these tags are the React JSX. To check it you just need to comment the line in which we have imported the React in index.js file to remember that, then the output will be:
In this, you can see the error react is not defined
, hence you can’t use JSX.
You need to wrap more than one element with one container element. This div container element has three nested elements.
import React, { Component } from 'react';
class App extends Component{
render(){
return(
<div>
<h1>Hello World!!!</h1>
<h2>Learn in easy way</h2>
<p>This website contains the best React tutorials.</p>
</div>
);
}
}
export default App;
We can also use custom HTML attributes and properties and standard HTML properties. Data prefix is required when adding custom attributes. We have added data-attribute, As an attribute of the p element in this example:
import React from 'react';
class App extends React.Component
{
render() {
return (
<div>
<h2>This is heading 1</h2>
<h3>This is heading 2</h3>
<p data-attribute = "firstattr">This is a Paragraph!!!</p>
</div>
);
}
}
export default App;
import React from 'react';
class App extends React.Component {
render() {
var heading = {
fontSize: 130,
color: 'blue'
}
return (
<div>
<h2 style = {heading}>React Guru</h2>
</div>
);
}
}
export default App;
React recommends using inline styles. We must use the camelCase syntax whenever we need to set up inline types. The above example illustrates how to add heading to element the h1.
import React, { Component } from 'react';
class App extends Component{
render(){
return(
<div>
<h1 className = "hello" >Hello World!!!</h1>
{/* This is a comment in JSX */}
</div>
);
}
}
export default App;
React JSX allows users to use remarks that start with /* and end with or */ and wrap them {in curly braces with curly braces, similar to the example for JSX expressions. This example illustrates how to make use of comments within JSX.
Inside JSX, JavaScript expressions can be utilized. You just wrap it up {in curly brackets}. This example illustrates rendering 2.
import React from 'react';
class App extends React.Component
{
render() {
return (
<div>
<h1>{ 10 + 20 }</h1>
</div>
);
}
}
export default App;
We aren’t able to utilize the conditional (ternary) expressions when other statements are in JSX instead. In the following example variable I is 1 thus the browser renders true. However, it’ll be false if we alter the value to another one.
import React from 'react';
class App extends React.Component
{
render() {
var i = 4;
return (
<div>
<h1>{i == 7 ? 'True!' : 'False'}</h1>
</div>
);
}
}
export default App;
[…] camelCase event handler and function is called in curly braces because we need to pass function in JSX […]
[…] of the Forms are pretty similar right?, because react used JSX, and we have already learned about JSX, and how is it similar to HTML? both forms look like we have copied and pasted them, nothing else. […]
[…] elements inside the div. As we know, div can contain many elements in the HTML code, similarly, In JSX <div> will be passed as a single element in the prop, so code will run […]
[…] style prop value must be an object. This is the error in react when we try to use style in the JSX element. Simply, when we try to pass style like in HTML, where we pass the attributes and values as […]