JSX Flashcards
What is JSX?
It is a syntax extension to JavaScript. It’s used to describe what the UI should look like. JSX produces React “elements”.
Why JSX?
React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display. Instead of artificially separating technologies by putting markup and logic in separate files, React separates concerns with loosely coupled units called “components” that contain both.
How can you embed expressions in JSX?
You can put any valid JavaScript expression inside the curly braces in JSX. For example, 2 + 2, user.firstName, or formatName(user) are all valid JavaScript expressions.
JSX is an expression
After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.
Where can you use JSX?
You can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions.
Specifying Attributes with JSX
You may use quotes to specify string literals as attributes. You may also use curly braces to embed a JavaScript expression in an attribute.
Specifying Children with JSX
If a tag is empty, you may close it immediately with />. JSX tags may contain children: const element = ( <div> <h1>Hello!</h1> <h2>Good to see you here.</h2> </div> );
JSX Prevents Injection Attacks
It is safe to embed user input in JSX. By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered. This helps prevent XSS (cross-site-scripting) attacks.
JSX Represents Objects
Babel compiles JSX down to React.createElement() calls. React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this:
// Note: this structure is simplified const element = { type: 'h1', props: { className: 'greeting', children: 'Hello, world!' } };
What are React “elements”?
React.createElement() performs a few checks to help you write bug-free code but essentially it creates an object like this: // Note: this structure is simplified const element = { type: 'h1', props: { className: 'greeting', children: 'Hello, world!' } };
These objects are called “React elements”. You can think of them as descriptions of what you want to see on the screen. React reads these objects and uses them to construct the DOM and keep it up to date.
Embedding map() in JSX
JSX allows embedding any expression in curly braces so we could inline the map() result:
function NumberList(props) { const numbers = props.numbers; return ( <ul> {numbers.map((number) =>
)} </ul> ); }
Sometimes this results in clearer code, but this style can also be abused. Like in JavaScript, it is up to you to decide whether it is worth extracting a variable for readability. Keep in mind that if the map() body is too nested, it might be a good time to extract a component.