React Flashcards

1
Q

What is a pure function?

A

A function is considered pure if it contains no side effects and if, given the same input, it always returns the same output.

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

When to abstract a chunk of code into its another file from React component?

A

If you have a component that’s being reused somewhere else, make it its own file. If not, just create it inside of whatever file you need it.

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

Where side effects should be handled in React?

A

Any side effects your component has should be wrapped inside of event handlers.

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

What is an expression in javaScript?

A

In JavaScript, an expression is any valid unit of code that evaluates to a value.

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

Why key prop is important while rendering a list?

A

When you give each list item a unique key prop, it helps React know which items, if any, change throughout different renders of that component.

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

How JSX looks like at runtime?

A

function Hello () {
return <h1>Hello World</h1>
}

at build time will be compiled to regular JavaScript like this

import { jsx as _jsx } from “react/jsx-runtime”

js
function Hello() {
  return _jsx("h1", {
    children: "Hello World"
  })
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What are props?

A

Props are to components what arguments are to functions.

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

What is a React Element?

A

An object representation of a DOM node

ex: <Icon></Icon>

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

What are hooks?

A

Hooks are functions, but it’s helpful to think of them as unconditional declarations about your component’s needs. You use React features at the top of your component similar to how you import modules at the top of your file.

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

When react renders a component?

A

React re-renders a component whenever its state changes.

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

When does React re-render?

A

React will only re-render when the state of a component changes

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

Important re-render aspect

A

React will only re-render once per event handler, even if that event handler contains updates for multiple pieces of state. (only if state updates are synchronous and not in async call like setTimeout)

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

When re-render happens?

A

Whenever state changes, React will re-render the component that owns that state and all of its child components – regardless of whether or not those child components accept any props.

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

What means a function having side effects?

A

A function has a side effect any time it does anything other than take some input, an argument, and calculate some output, a return value.

Adjusted for React, a component has a side effect any time it does anything other than take some input, props and state, and calculate some output, a View.

API calls, manual DOM manipulation, using browser APIs like localStorage or setTimeout, or anything else that falls outside of simply calculating a View based on props and state is a side effect.

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

React Rule #0

A

When a component renders, it should do so without running into any side effects.

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

React Rule #1

A

If a side effect is triggered by an event, put that side effect in an event handler.

17
Q

What is lazy state initialization?

A

The idea is you can pass useState a function that will only be called once, on the initial render, and the return value of that function will be used as the initial value for that state.

Something like:

const [index, setIndex] = React.useState(() => {
    return Number(localStorage.getItem("index"))
  })
18
Q

React Rule #2

A

If a side effect is synchronizing your component with some external system, put that side effect inside useEffect.

19
Q

When useEffect should be used?

A

useEffect should be used for synchronizing, not for reacting to changes in a value.

20
Q

What is cleanup function?

A

If you return a function from your effect, React will call that function each time before it ever calls your effect again, and then one final time when the component is removed from the DOM.

21
Q

What means reactive values in React?

A

A reactive value is any value that can change between re-renders. Props, state, or any variables defined inside of a component are all reactive values.