React and the DOM Flashcards

1
Q

What does the following JSX become when transpiled to JS?

<h1>Hello World</h1>

A

React.createElement(‘h1’, null, ‘Hello World’)

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

What are the three main differences between HTML and JSX?

A
  1. Tag attributes are camel cased.
  2. All elements must be balanced.
  3. The attribute names are based on the DOM API, not on the HTML language specs
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why must all JSX elements be balanced?

A

Because it’s XML

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

What does “all JSX elements need to be balanced” mean in practice?

A

Tags such as <br></br> and <img></img>, which don’t have ending tags,

need to be self-closed. So, instead of <br></br>, use <br></br> and instead of <img></img>, use <img></img>

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

What does “the attribute names are based on the DOM API” mean in practice?

A

When interacting with the DOM API, tag attributes may have different names than those you use in HTML. One of such example is class and className. e.g.

<div></div>

if you want to manipulate the DOM and change its class name using plain JavaScript, you’d do:

document.getElementById(“box”).className=”some-other-class”

As far as JavaScript is concerned, that attribute is called className, not class. Since JSX is just a syntax extension to JavaScript, it conforms to the attribute names as defined in the DOM. That same div should be expressed in JSX as:

return <div></div>

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

Why doesn’t this work?

return (

<h1>Hello World</h1>

<h2>Have a nice day</h2>

)

A

It’s the equivalent of returning two things in JS which you’re not allowed to do?

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

How can we solve the below problem:

return (

<h1>Hello World</h1>

<h2>Have a nice day</h2>

)

A

Return a single DOM node:

return (
 <div>
 <h1>Hello World</h1>
 <h2>Have a nice day</h2>
 </div>
)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Can we use if statements in JSX?

A

No, it gets converted into invalid JS

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

What are the two alternatives to using if statements in JSX?

A

Use a ternary operator or take the if statement out of the JSX

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

How does React/JSX handle null and undefined?

A

It just outputs nothing

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

How will React render this:

return (
<a>Google</a>
<a>Facebook</a>
)

A

With no space between the links (unlike HTML where the browser would put a space in)

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

How can we render a space with React?

A

{‘ ‘}

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

How do we create a comment in JSX?

A

{/* my comment here */}

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

What is React’s built-in XSS attack protection?

A

By default it won’t allow HTML tags to be generated dynamically and attached to JSX

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

Give an example of when you might want to generate HTML on the fly?

A

Rendering data in markdown format to the interface

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

What is Markdown

A

A format which allows you to write using an easy-to-read, easy-to write plain text format

17
Q

What does React provide to skip XSS protection and render anything directly?

A

The dangerouslySetInnerHTML property

18
Q

What are some benefits of using inline styles with React?

A
  1. Scoped styles without selectors
  2. Avoids specificity conflicts
  3. Source order independence