React Flashcards

1
Q

What is React?

A

React is a JavaScript library for building User Interfaces. It is not a framework.

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

What is React responsible for?

A

React is only responsible for the View layer.

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

How do you install React?

A

Install react with npm install react

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

How do you put React into a file?

A

Import React in every file you need it with import React from “react”

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

What is a React Element?

A
  • A React Element is the smallest building block. It’s a representation of a small piece of your UI.
  • React.createElement returns a React Element:
    React.createElement(type, options, children)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is ReactDOM?

A

ReactDOM is the glue between React and the DOM. ReactDOM is separate from React because you can write React for native applications.

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

What is reconciliation?

A

Reconciliation is the process of syncing the Virtual DOM to the actual DOM.

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

How do you install ReactDOM?

A
  • Install ReactDOM with npm install react-dom
  • Import ReactDOM’s createRoot method with import {createRoot} from “react-dom/client”
  • The root element is where ReactDOM will render your UI
    createRoot(root).render(element);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What manages the root element?

A
  • ReactDOM completely manages the root element
  • Apps built with React have a single root element (The most common use case)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is JSX?

A
  • JSX is a special syntax for React that makes it easier to represent your UI
  • JSX looks similar to HTML but it is not HTML
  • JSX code you write gets transformed into React.createElement
  • JSX is not part of your browser. You need a tool to transform it into valid JavaScript.
  • A JSX element is an object.
  • You can treat a JSX element like an object.
  • You can assign a JSX element to a variable.
  • You can return a JSX element from a function.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do attributes get passed in JSX?

A
  • Attributes in JSX get passed as the 2nd argument of React.createElement(…)
  • <div></div>is how you would give the class active to this element.
  • You need quotes around attribute values that are strings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is an expression?

A
  • An expression is any valid JavaScript code that resolves to a value
  • Expressions in JSX need to be wrapped inside curly braces: {expression}

<h1>You have {2 + 3} notifications</h1>

is an example of JSX with an expression.

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

What is a React Component?

A
  • A React Component is a function that returns one React Element describing a section of the UI.
  • Components defined with a function are called function components.
  • React Components promote code reuse and are easier to debug.
  • A React Component’s name has to start with an uppercase.
  • Use UpperCamelCase when naming React Components
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are props?

A
  • Props is short for properties.
  • Props is an object received as the first argument of a Component.
  • Attributes on Components get converted into an object called Props.
  • Props make components more reusable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What are children?

A
  • props.children represents the content between the tags of a Component.
  • props.children can be an array or a single item.
  • props.children can contain text and/or React Elements and/or React Components.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What shouldn’t React Components do?

A
  • All React Components should never modify their props.
  • React expects Components to be pure so that it can efficiently update the DOM only when/where necessary.
17
Q

How do you use clsx?

A
  • You can install clsx with npm install clsx
  • clsx is a tiny utility for constructing className strings conditionally.
  • clsx({“your-class-name”: booleanValue}) is the generic syntax for clsx.
18
Q

What is array destructuring?

A

Array destructuring is a shorter syntax for creating variables out of the items of an array.

Array destructuring works regardless of the data types in the array.

19
Q

What is a State?

A
  • State refers to any variable defined inside a Component with the intent to update later.
  • State variables can be updated from inside the component.
  • When a state variable is updated, the component automatically re-renders.
20
Q

What is useState?

A
  • useState allows us to create a state variable in a Component
  • useState is a named export that needs to be imported
  • You can import useState and React: import React, {useState} from “react”;
  • useState is one of many React Hooks.
21
Q

How does useState work?

A
  • The useState() function takes the initial_value as the only argument.
  • The useState() function returns an array of 2 items
  • Always destructure the useState into abc and setAbc where abc is the name of the state
  • The default value of the state variable returned by useState will be the same as the initial_value passed to useState().
22
Q

What happens when you change state?

A

When you change the state, React will call the Component function again to re-render

23
Q

What’s a closure?

A
  • A closure is where an inner function has access to the outer function’s variables.
  • Functions in JavaScript have access to their outer scope. This is called closures.
  • Closures is what you get when you define a function; it’s not something you have to “enable” or decide to use.
  • Functions defined inside another function can use the variables defined in the outer function.
24
Q

What’s conditional state change?

A

Conditional state change is when you wrap the state set function with an if condition to satisfy some business logic.

25
Q

What’s the logical && operator?

A
  • The logical && operator is a JavaScript feature that allows chaining two expressions. The 2nd expression will only execute if the first one returns true.
  • Using the logical && operator in JSX makes it easier to render elements or components conditionally.
26
Q

What are the two rules of Hooks?

A
  • Rules of hooks #1: Only call Hooks from React functions
  • Rules of hooks #2: Only call Hooks at the Top Level and never call hooks inside loops, conditions, or nested functions.
  • React relies on the order in which Hooks are called
27
Q

Explain arrays.

A
  • Arrays are objects in JavaScript.
  • [] === [] is the same as new Array() === new Array()
  • {} === {} is the same as new Object() === new Object()
  • [] === [] is false because it’s comparing 2 different instances of arrays
  • {} === {} is false because it’s comparing 2 different instances of objects
  • When you assign a variable to an object or array, it does NOT copy it. It will reference its value.
28
Q

What’s an immutable array?

A

An immutable object is one that cannot be changed. As we cannot (or sometimes, should not) modify an immutable object, we create an entirely new object based on the original, leaving the original object unchanged.

29
Q

What does the spread operator do?

A
  • The spread operator in an array creates a shallow copy.
  • […existingArray, “newValue”] adds newValue to the array in an immutable way because it creates a copy of it.
30
Q

How do you update an array in an immutable way?

A

.map helps you update an array in an immutable way.

31
Q

Why should you use .slice and not .splice?

A

.slice is immutable, whereas .splice mutates the array.

32
Q

How do you remove items from an array in an immutable way?

A

.filter helps you remove items from an array in an immutable way.

33
Q

What should you do whenever you iterate with .map in JSX?

A
  • Whenever you iterate with .map in JSX, you should provide a key.
  • The key should be unique. As a last resort, you can use the index of the map iteration (which is the 2nd argument)
  • The key allows React to update the DOM efficiently with the least amount of operations.
34
Q

What’s an easy way to immutably remove a key from an object?

A

const {keyToRemove, …rest} = obj is an easy way to immutably remove keyToRemove from an object.

35
Q
A