How React Works Flashcards

1
Q

In React we create apps using what king of JS syntax?

A

JSX

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

The most atomic units of React are …..

A

React Elements

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

HTML elements relate to one another in ……

A

in a hierachy that resembles a family tree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
<section id="baked-salmon">
  <h1>Baked Salmon</h1>
  <ul class="ingredients">
    <li>2 lb salmon</li>
    <li>5 sprigs fresh rosemary</li>
    <li>2 tablespoons olive oil</li>
    <li>2 small lemons</li>
    <li>1 teaspoon kosher salt</li>
    <li>4 cloves of chopped garlic</li>
  </ul>
  <section class="instructions">
    <h2>Cooking Instructions</h2>
    <p>Preheat the oven to 375 degrees.</p>
    <p>Lightly coat aluminum foil with oil.</p>
    <p>Place salmon on foil</p>
    <p>Cover with rosemary, sliced lemons, chopped garlic.</p>
    <p>Bake for 15-20 minutes until cooked through.</p>
    <p>Remove from oven.</p>
  </section>
</section>

How many root elements are there and how many children are there?

A

One root element (in this case, a section) - which has three children: a heading, an unordered list of ingredients, and a section for the instructions.

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

In web development SPA stands for ?

A

Single-Page Applications

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

What features of JS lead to ease of developing SPA ?

A

AJAX
Asynchronous JS and XML

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

The DOM API is a collection of ….

A

Objects

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

DOM is made up of DOM elements; and the React DOM is made up of ……

A

React Elements

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

A React element is a description of ….

A

What the actual DOM element should look like.

In other words, React elements are the instructions for how the browser.

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

What would creating the following HTML look like in React?
```<h1 id="recipe-0">Baked
Salmon</h1>
~~~

A
React.createElement("h1", { id: "recipe-0" }, "Baked Salmon");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the arguments in the following reactJS expression?
```React.createElement(“h1”, { id: “recipe-0” }, “Baked Salmon”);
~~~

A
  • The first argument defines the type of element we want to create. eg. h1 element.
  • The second argument represents the element’s properties in this case id of recipe-0.
  • The third argument represents the element’s children: any nodes that are inserted between the opening and closing tag
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would the JS literal for the following DOM element look like?
```<h1 id="recipe-0">Baked Salmon</h1>
~~~

A
{
  \$\$typeof: Symbol(React.element),
  "type": "h1",
  "key": null,
  "ref": null,
  "props": {id: "recipe-0", children: "Baked Salmon"},
  "_owner": null,
  "_store": {}
}

- These are fields used by React to construct the DOM element.

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

How are react elements created and rendered?

A
  1. To created element - use the React.createElement( )
  2. To render use ReactDOM.render()
    ~~~
    const dish = React.createElement(“h1”, null, “Baked Salmon”);

ReactDOM.render(dish, document.getElementById(“root”));
~~~

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

What are the arguments for the ReactDOM.render( ) ?

A
  1. The first argument is the DOM element to be rendered?
  2. The second element is the “root” or node where the element is inserted.
    ~~~
    const dish = React.createElement(“h1”, null, “Baked Salmon”);

ReactDOM.render(dish, document.getElementById(“root”));
~~~

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

What would be the JS literal for the following DOM element?
```<ul>

<li>2 lb salmon</li
<li>5 sprigs fresh rosemary</li>
<li>2 tablespoons olive oil</li>
<li>2 small lemons</li><li>1 teaspoon kosher salt</li><li>4 cloves of chopped garlic</li>
</ul>
~~~
</li>

A
{
    "type": "ul",
    "props": {
    "children": [
    { "type": "li", "props": { "children": "2 lb salmon" } … },
    { "type": "li", "props": { "children": "5 sprigs fresh rosemary"} … },
    { "type": "li", "props": { "children": "2 tablespoons olive oil" } … },
    { "type": "li", "props": { "children": "2 small lemons"} … },
    { "type": "li", "props": { "children": "1 teaspoon kosher salt"} … },
    { "type": "li", "props": { "children": "4 cloves of chopped garlic"} … }
    ]
    ...
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Given the following data set how would you create ul element with its indexes making up the li items using React.createElement() ?
~~~
const items = [
“2 lb salmon”,
“5 sprigs fresh rosemary”,
“2 tablespoons olive oil”,
“2 small lemons”,
“1 teaspoon kosher salt”,
“4 cloves of chopped garlic”
];
~~~

A

One can try:
~~~
React.createElement(
“ul”,
{ className: “ingredients” },
items.map((ingredient, i) =>
React.createElement(“li”, { key: i }, ingredient)
)
);
~~~

17
Q

what is the syntax for a react class component?

A
class IngredientsList extends React.Component {
  render() {
    return React.createElement(
      "ul",
      { className: "ingredients" },
      this.props.items.map((ingredient, i) =>
        React.createElement("li", { key: i }, ingredient)
      )
    );
  }
}
18
Q

What is JSX?

A

A JS extention that allows us to define React elements/components using a tag-based syntax

19
Q

Inside JSX JS expression are wrapped in …

A

curly braces
~~~

<h1>{title}</h1>

~~~

20
Q

Before JSX can be intepreted by a browser it needs to ….

A

To be converted to JavaScript

This process is called compiling, and it’s what compilers like Babel are

21
Q

Given the following data set outline how you can create react components that display it as ul ements from it?

const data = [
  {
    name: "Baked Salmon",
    ingredients: [
      { name: "Salmon", amount: 1, measurement: "l lb" },
      { name: "Pine Nuts", amount: 1, measurement: "cup" },
      { name: "Butter Lettuce", amount: 2, measurement: "cups" },
      { name: "Yellow Squash", amount: 1, measurement: "med" },
      { name: "Olive Oil", amount: 0.5, measurement: "cup" },
      { name: "Garlic", amount: 3, measurement: "cloves" }
    ],
    steps: [
      "Preheat the oven to 350 degrees.",
      "Spread the olive oil around a glass baking dish.",
      "Add the yellow squash and place in the oven for 30 mins.",
      "Add the salmon, garlic, and pine nuts to the dish.",
      "Bake for 15 minutes.",
      "Remove from oven. Add the lettuce and serve."
    ]
  },
  {
    name: "Fish Tacos",
    ingredients: [
      { name: "Whitefish", amount: 1, measurement: "l lb" },
      { name: "Cheese", amount: 1, measurement: "cup" },
      { name: "Iceberg Lettuce", amount: 2, measurement: "cups" },
      { name: "Tomatoes", amount: 2, measurement: "large" },
      { name: "Tortillas", amount: 3, measurement: "med" }
    ],
    steps: [
      "Cook the fish on the grill until cooked through.",
      "Place the fish on the 3 tortillas.",
      "Top them with lettuce, tomatoes, and cheese."
    ]
  }
];
A

Create a series of function components that pass the data as props from the parent to children:
~~~
function Recipe({ name, ingredients, steps }) {
return (
<section id={name.toLowerCase().replace(/ /g, “-“)}>
<h1>{name}</h1>
<ul className="ingredients">
{ingredients.map((ingredient, i) => (
<li key={i}>{ingredient.name}</li>
))}
</ul>
<section className="instructions">
<h2>Cooking Instructions</h2>
{steps.map((step, i) => (
<p key={i}>{step}</p>
))}
</section>
</section>
);
}

function Menu({ title, recipes }) {
return (
<article>
<header>
<h1>{title}</h1>
</header>
<div className="recipes">
{recipes.map((recipe, i) => (
<Recipe key={i} {…recipe} />
))}
</div>
</article>
);
}

ReactDOM.render(
<Menu recipes={data} title=”Delicious Recipes” />,
document.getElementById(“root”)
);
~~~