What is JSX?
- In this article, we will learn together about what ReactJS JSX is, what it does and how it is structured. Why look like HTML but don’t call it HTML, what makes it different?
- Let’s find out together
What Happens with JSX?
- In this article, what we are interested in is what happens after we write a JSX paragraph. The way the browser understands JSX and converts it into regular HTML.
- Let’s take a look at an example of a component Card:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Card extends React.Component { render() { var cardStyle = { height: 200, width: 150, padding: 0, backgroundColor: "#FFF", WebkitFilter: "drop-shadow(0px 0px 5px #666)", filter: "drop-shadow(0px 0px 5px #666)" }; return React.createElement( "div", { style: cardStyle }, React.createElement(Square, { color: this.props.color }), React.createElement(Label, { color: this.props.color }) ); } }; |
- At first glance we know right away what is the JSX syntax:
1 2 3 4 5 | <div style={cardStyle}> <Square color={this.props.color}/> <Label color={this.props.color}/> </div> |
- We already know that our web browsers can’t understand JSX code, they don’t even know what JSX is. Even if we try to attach a specific description of JSX, that is why we need to use the Babel library to translate JSX into something the browser can understand: Javascript.
- That means the above JSX code after translation, it will become a pure Javascript like this:
1 2 3 4 5 6 7 | return React.createElement( "div", { style: cardStyle }, React.createElement(Square, { color: this.props.color }), React.createElement(Label, { color: this.props.color }) ); |
- All components, this property is called in the createElement constructor with initialization values. And here is the whole JavaScript code of the component Card after converting into javascript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Card extends React.Component { render() { var cardStyle = { height: 200, width: 150, padding: 0, backgroundColor: "#FFF", WebkitFilter: "drop-shadow(0px 0px 5px #666)", filter: "drop-shadow(0px 0px 5px #666)" }; return React.createElement( "div", { style: cardStyle }, React.createElement(Square, { color: this.props.color }), React.createElement(Label, { color: this.props.color }) ); } }; |
- And that answers our original question, how does one of our JSX snippets work? It will be transformed from JSX to Javascript in a very friendly way.
Evaluating Expressions
- JSX is similar to Javascript. And of course JSX also doesn’t have to bind us to fixed content:
1 2 3 4 5 6 7 8 | class Stuff extends React.Component { render() { return ( <h1>Boring static content!</h1> ); } }; |
- We can still do a few other things, such as giving a random number, our job is to put that expression into braces {}.
1 2 3 4 5 6 7 8 | class Stuff extends React.Component { render() { return ( <h1>Boring {Math.random() * 100} content!</h1> ); } } |
- And of course Math.random () * 100 is an expression that takes a random number, and if we don’t include it in {}, it will just be a piece of text. This reminds only everyone knows it.
Returning Multiple Elements
- In many previous examples. We often return elements, like a div. We can actually do more like return on many different elements.
- There are 2 possible ways to do so:
- The first way is that we use the same syntax as Array:
1 2 3 4 5 6 7 8 9 10 11 12 | class Stuff extends React.Component { render() { return ( [ <p>I am</p>, <p>returning a list</p>, <p>of things!</p> ] ); } } |
- Above, we return 3 ‘p’ tags, Between them there is no common 1 element. Now we will return so many times, and here one thing to note is that when returning 3 p tags they have nothing to distinguish so it can be confusing in the purpose of reuse later. this.
- So we should add one key to distinguish them:
1 2 3 4 5 6 7 8 9 10 11 12 | class Stuff extends React.Component { render() { return ( [ <p key="1">I am</p>, <p key="2">returning a list</p>, <p key="3">of things!</p> ] ); } }; |
- This helps React understand, specifically in each element that you return, which is changing, which is not. And of course if we don’t need to do anything, we don’t need to add the Key. If you forget, React will also remind you through the Dev Tools Console.
- Something like: Warning: Each child in an array or iterator should have a unique “key” prop.
- The second way we can do it, not to declare the type [] is to use React.Fragment.
- The code above will become:
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Stuff extends React.Component { render() { return ( <React.Fragment> <p>I am</p> <p>returning a list</p> <p>of things!</p> </React.Fragment> ); } }; |
- But there are a few things to keep in mind when using Fragment:
- This component does not actually generate a DOM element, they are just a JSX and cannot be converted into HTML.
- It doesn’t have the syntax like an Array, so we write them without using the sign
- No need to specify a key.
- Instead of calling <React.Fragment and </ React.Fragment> we write simply as <> and </>.
Can’t Specify CSS Inline
- In the previous post about styling in React we know that JSX is different from HTML and you won’t be able to write inline CSS like:
1 2 3 4 | <div style="font-family:Arial;font-size:24px"> <p>Blah!</p> </div |
With JSX, we have to define it separately:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Letter extends React.Component { render() { var letterStyle = { padding: 10, margin: 10, backgroundColor: this.props.bgcolor, color: "#333", display: "inline-block", fontFamily: "monospace", fontSize: "32", textAlign: "center" }; return ( <div style={letterStyle}> {this.props.children} </div> ); } } |
Comment code
- Regarding block comments, JSX is similar to JS. But notice a little bit, if we comment into an element then we still have to add the braces {} to React know this is the comment part.
1 2 | {/* I am a child comment */} |
- And if used for multiple lines and inside a tag, it will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 | ReactDOM.render( <div className="slideIn"> <p className="emphasis">Gabagool!</p> <Label /* This comment goes across multiple lines */ className="colorCard" // end of line /> </div>, document.querySelector("#container") ); |
Conclude
- In this article, we have a quick look at how our browser can understand JSX and some notes when using JSX. It looks like HTML but it is not.
- Thank you for watching and have a great day!