JSX Flashcards

1
Q

What is JSX?

A

It is a syntax extension to JavaScript. It’s used to describe what the UI should look like. JSX produces React “elements”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why JSX?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can you embed expressions in JSX?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

JSX is an expression

A

After compilation, JSX expressions become regular JavaScript function calls and evaluate to JavaScript objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Where can you use JSX?

A

You can use JSX inside of if statements and for loops, assign it to variables, accept it as arguments, and return it from functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Specifying Attributes with JSX

A

You may use quotes to specify string literals as attributes. You may also use curly braces to embed a JavaScript expression in an attribute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Specifying Children with JSX

A
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>
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

JSX Prevents Injection Attacks

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

JSX Represents Objects

A

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!'
  }
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are React “elements”?

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Embedding map() in JSX

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly