React Flashcards

1
Q

What is React?

A

A JavaScript library used in web development to build interactive elements on a website using UI components.

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

What makes React so great?

A

With React, we can setup data separately, iterate over the data, return the elements we want to render in the browser, and inject data dynamically.

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

Component

A

Independent chunks of user interfaces. There is at least one. Created with a function.

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

node_modules (folder)

A

contains dependencies the project is using. Also contains sub-dependencies.

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

public (folder)

A

where the index.html is that displays the webpage.

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

src (folder)

A

the brain of the application. Where all the work is performed in the application. src/index.js is the JavaScript entry point.

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

.gitignore (file)

A

specifies which files source control (Git) should ignore

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

package.json (file)

A

every Node.js project has this file and it contains information about the project. For example, a list of dependencies and scripts.

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

package-lock.json (file)

A

a snapshot of the entire dependency tree. Shows the main dependencies the project is using and the scripts. This shows all the dependency packages with specific versions so when someone uses this project, they’ll have the exact dependency tree with all the same dependency versions.

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

JSX (JavaScript XML)

A

A syntax extension to JavaScript which allows you to write HTML in React. JSX converts HTML tags into react elements. So you do not have to use HTML methods such as createElement() and/or appendChild(). You are not required to use JSX, but it makes it easier to write React applications. You can only have one parent element in JSX.

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

Initial Render

A

In a React app, the initial render is the first time the component tree is rendered to the DOM. This happens when the application first loads, or when the root component is rendered. This is known as “mounting” the components. When the component us unmounted and re-mounted, it will also be initially rendered.

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

re-render

A

This happens when the component’s state or props change and the component needs to be updated in the DOM to reflect the changes. Re-renders also happen when the parent element re-renders. React uses a virtual DOM to optimize the process of updating the DOM so only the necessary changes are made.

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

<React.Fragment>
</React.Fragment>

A

lets you group a list of children without adding extra nodes to the DOM. You won’t see this tag in inspect element. The shorthand for this is <> </>

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

className (attribute)

A

attribute that is used since “class=’xxx’ “ will not work.

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

react dev tools (chrome extension)

A

You can see the component tree when opening inspect element > Components (tab)

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

import “./xxxx.css” (code)

A

this allows you to import a css file into the .js file. You do not have to do this with .js files.

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

expression vs statement

A

an expression returns a value and statement does not. In JSX, the value inside {} must be an expression.

18
Q

props (property)

A

Props are used to store data that can be accessed by child components. You provide the prop where you render the component. Ex: <List people={person} />

19
Q

child prop

A

a special prop automatically passed to every component. it is used to display whatever you include between the opening and closing tags when invoking a component.

20
Q

key prop

A

a mechanism for controlling component instances. You’ll need to set the key prop to unique values. Ex. id: 0, Id: 1…

21
Q

events

A

For events, you’ll need three things. An element, an event (onClick, onChange, etc), and a reference or callback function to do something.

22
Q

prop drilling

A

in react, you can only pass props down from parent to child.

23
Q

optimization

A

when files (images, etc) are stored in the src folder, create-react-app optimizes the files when the react app is built. Optimization doesn’t happen when files are stored in the public folder.

24
Q

npm run build (cmd)

A

stop the dev server with “ctrl + c” > run “npm run build” > a build folder will be created with static files.

25
Q

rmdir /Q/S node_modules

A

windows cmd to remove the node_modules directory.

26
Q

JSX

A

a JavaScript extension syntax used in React to easily write HTML and JavaScript together.

27
Q

list of data

A

You will need to specify an “id: INTEGER” when rendering a list of data.

28
Q

hooks

A

Functions React provides to help complete various tasks.
RULES:
- React and custom hooks start with the keyword “use…”.
- The component name must be uppercase.
- The hook must be invoked in the component body.
- You cannot call hooks conditionally (ex. Within an if statement).
- Set functions don’t update the state immediately.
ex. setState(NEW_VALUE)
console.log(state) // This will still show the original value

29
Q

State (hook)

A

A special function that takes an initial state as an argument and returns an array with two elements. “state” is the default value, and “setState” is the function that controls the “state” value. When setState() is invoked, it preserves the value between renders and re-renders the page.

Syntax: “const [state, setState] = useState(INITIAL_STATE)”.
Naming convention for boolean: [isState, setIsState]

Additional note: When using the “setState” function, you can add a callback function to access the current state value as well as the new state value as soon as the current state value is changed. The first parameter in the callback function is automatically the current state value. Example:
setState((currentStateValue) => {
const newStateValue = currentStateValue + 1;
console.log(newStateValue);
return newStateValue;
})

30
Q

Effect (hook)

A

Allows you to perform side effects outside of the component.
- By default, it runs on each render (initial and re-render).
- It takes two arguments (second optional)
- first argument = a callback function
- second argument = dependency array
- callback functions can’t return a promise (so you can’t make it async)
- if a dependency array is empty [], it will only run on the initial render.
- If an element is passed into the dependency array, and the value changes, useEffect will run.

Syntax: “useEffect(callback function, [dependancy_array])”.

Side effects such as - fetching data, updating the DOM, event listeners, timers, etc.

31
Q

useEffect (hook) - run on initial render

A

“useEffect(callback function, [])” the blank array makes it so the hook is only run once.

32
Q

useEffect (hook) - cleanup function

A

Within useEffect, you can add a “return () => {code_here}” to prevent memory leaks by cleaning up useEffects. Every time you create a side effect, you want to add a clean-up function. The return callback is called when the component is unmounted.

Example - the clean-up function is within the return callback:
useEffect(() => {
window.addEventListener(“resize”, checkSize);
return () => {
window.removeEventListener(“resize”, checkSize);
};
}, []);

33
Q

npx create-react-app NEW_APP_NAME (cmd)

A

cmd tool to create single-page React applications officially supported by the React team. Another option to start a react app is using ViteJS (see vitejs.dev flashcard)

34
Q

vitejs.dev (pronounced veet)

A

A website that provides another way to implement create-react-app faster. See https://github.com/john-smilga/react-course-v3/blob/main/01-fundamentals/README.md > at “Vite Install” and “Vite Setup” for how to use it. You MUST name all .js files .jsx

35
Q

Display elements conditionally in React

A

You cannot use if statements since they don’t return a value. You need to use short-circuit evaluation or a ternary operator.

Short-circuit evaluation is good for showing no items or one item.

Ternary operator is good for showing one item or another item.

36
Q

Why use an anonymous function for event handlers?

A

If you don’t wrap it in an anonymous function, the function will be called on the component mount.

Example:
button onClick= { add(1, 2) }
VS
button onClick { () => add(1, 2) }

The first one will call the function when the component is mounted. The second will call the function when the button is clicked.

37
Q

batching

A

Refers to the process of grouping multiple state updates into a single update. This is useful because it allows React to optimize the rendering of your components by minimizing the number of DOM updates needed.

38
Q

auto-batching

A

A default technique react uses to group state updates that occur within the same event loop into a single update. This means if you call the state update function multiple times in a short period of time, React will only perform a single re-render for all the updates. Example: When you call “useState(xx), useState(xx)” multiple times in the same event loop.

39
Q

react icons

A

To access react icons - https://react-icons.github.io/react-icons
Install the react-icons library.

  1. find an icon you want and then click on it to copy the component name.
  2. In your code, import the icon.
    - “import { ICON_NAME } from ‘react-icons/XXX’”
  3. Reference the icon as a component <ICON_NAME></ICON_NAME>

You can style it by adding a className to the component or target it since it is returned as an svg tag.

40
Q

Passing props

A

When you pass prop(s) from a parent to child- it is passed as a single object. In the child prop, you need to destructure the single prop or reference the property.