Toptal Tutorial 2/19 Flashcards

1
Q

What is React?

A

A declarative component-based view library that helps one to build a UI.

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

What would be a React Framework?

A

By using React, Redux, and React router, we have ll the necessary to make a single page application.

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

Important Thing #1: React uses JSX, what is JSX?

A

JSX is a javascript syntax extension that somewhat resembles HTML/XML

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

Important Thing #2: React is based on the virtual DOM.

A

The virtual DOM is memory of an ideal tree that is represented by the javascript the developer writes that is later compared with the real DOM and synced with it via a process called reconciliation.

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

Current demand for React experts is?

A

React has roughly 6-10 times more job openings than Vue and 2-4 times more job openings than Angular. Why then does Vue have more stars than react on GitHub, but way fewer job openings??? No inkling why this is…..

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

Comparing React, Angular and Vue - who is behind their development?

A

React = Facebook, Angular = Google, Vue = open source.

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

Components can be either a function or a class. What is the difference?

A

Class has state and can use refs, lifecycle, and hooks whilst a function component is stateless.

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

What are the 2 types of class components and what is the difference between them?

A

Component and PureComponent. A PureComponent does a shallow comparison of props and state (reduced overhead), and Component will make full comparisons. Component can be used with shouldComponentUpdate.

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

Name the 7 react lifecycle methods.

A

Constructor(props)
componentDidMount()
componentWillUnmount()
componentDidUpdate(prevProps, prevState, snapshot)
shouldComponentUpdate(nextProps, nextState)
getSnapshotBeforeUpdate()
componentDidCatch(error, info)

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

Name 2 extra methods that are static.

A
static getDerivedStateFromError(error)
statis getSnapshotBeforeUpdate(props, state)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe Constructor(props)

A
  • Optional, especially with CRA being that popular, where class field declarations for JavaScript are included as default. It is pointless to declare if you are binding your methods by arrow function within the class body. A similar state can be also initialized as a class property.
  • Should be used only for initializing local state for object and binding methods in ES6 classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe componentDidMount()

A
  • Make Ajax calls here.
  • If you need event listeners, subscriptions, and such, add them here.
  • You can use setState here (but it will make the component rerender).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe componentWillUnmount()

A
  • Cleans all stuff that is still ongoing—e.g., Ajax should be interrupted, subscription unsubscribed, timers cleared, and so on.
  • Do not call setState, as it is pointless because the component will be unmounted (and you will get a warning).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe componentDidUpdate(prevProps, prevState, snapshot)

A
  • Happens when the component just finished updating (doesn’t happen on initial render).
  • Has three parameters that are optional to use (previous props, previous state, and a snapshot that will only appear if your component implements getSnapshotBeforeUpdate).
  • Only happens if shouldComponentUpdate returns true.
  • If you use setState here, you should guard it or you will land in an infinite loop.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Describe shouldComponentUpdate(nextProps, nextState)

A
  • Only for performance optimization.
  • If it returns false, then a render will NOT be invoked.
  • PureComponent can be used instead if the overridden SCO is just shallow props/state comparison.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Describe getSnapshotBeforeUpdate()

A

Can be used for keeping some information about current DOM, e.g., current scroll position which later can be reused within componentDidUpdate to restore the position of the scroll.

17
Q

Describe componentDidCatch(error, info)

A
  • A place where logging errors should happen.
  • Can call setState, but in future releases, it will be dropped in favor of the static method getDerivedStateFromError(error), which will be updating the state by returning a value to update the state.
18
Q

Describe static getDerivedStateFromError(error)

A
  • Error information is available here.
  • Should return an object value that will update the state which can be used for handling errors (by displaying something).
  • As it is static, it doesn’t have access to the component instance itself.
19
Q

Describe static getSnapshotBeforeUpdate(props, state)

A
  • Should be used in cases where props change over time—as an example, according to React docs, it might be useful for a transition component.
  • As it is static, it doesn’t have access to the component instance itself.
20
Q

What are Props?

A

Props are properties that are passed to the component which later can be reused within it for displaying information/business logic and such:

import React, { Component } from ‘react’;

export default class App extends Component {
   render() {
   return (
       <div>

       </div>
   );    } }

const HelloWorld = (props) => <div>Hello {props.name}</div>

21
Q

Tell more about Props.

A
  • Props are read-only elements and cannot be changed directly in child components
  • not only properties like strings can be passed to child components, but also numbers, objects, functions, etc.
  • Props have also one more useful thing which is called defaultProps, a static field which can tell you what the default props are for a component (when they are not passed down to the component, for example).
  • In case of “lifting state up,” where one component (the parent) has a state which is later being reused by its children (e.g., one child displays it and another allows editing), then we need to pass the function to child from parent, which allows us to update the parent’s local state.
22
Q

What is state?

A

State, on the other hand, is a local state that can be modified, but indirectly by using this.setState

23
Q

Describe SetState

A

SetState is a method to change local state object (by doing a shallow merge), and after that, the component responds that by rerendering itself. Be aware that after setState is used, the this.state property won’t reflect the changes mentioned in function right away (it has an asynchronous nature) as a few instances of setState might be batched together due to optimization. It

24
Q

What is the React Context API?

A
  • Was an experimental feature for a long time, but now available in stable version.
  • helps us solve one issue: props drilling.
  • Before Context (or rather before it became non-experimental), it was drilled down by passing in a recursive way from parent to child through to the last leaf (obviously there was Redux which also could solve the issue). Be aware that this feature solves ONLY props drilling and isn’t a replacement for things like Redux or Mobx. Obviously, if you were using a state management library only for that, then you can replace it freely.
25
Q

Describe the difference between controlled and uncontrolled components in React.

A

The value of controlled components, as their name suggests, is controlled by React by providing a value to the element that interacts with the user, while uncontrolled elements do not get a value property.

26
Q

What is valuable about the controlled component?

A

we have a single source of truth which happens to be the React state, so there is no mismatch between what we are seeing on the screen and what we currently have in our state.

27
Q

How do we find the state value in an uncontolled component?

A

In uncontrolled React components, we do not care about how the value changes, but if we want to know the exact value, we simply access it through ref.

28
Q

What will replace refs in version 16.8?

A

Hooks

29
Q

What are error boundaries?

A
  • If something goes wrong and there’s no error boundary as its parent, it will result in the whole React app failing.
  • With error boundaries, you have an added degree of flexibility that you can use. You can either use one across the whole app and display an error message, or use it in some widgets and simply not display them, or display a small amount of information in place of those widgets instead.
  • Remember that it is only about issues with declarative code rather than imperative code that you write for handling some events or calls. For these, you should still use the regular try/catch approach.
  • Error boundaries are also a place where you can send information to the Error Logger that you use (in the componentDidCatch lifecycle method).
30
Q

What are Higher Order Components?

A

HOCs are just functions that take a component as an argument and will return a new component with extended capabilities compared to the one without the HOC wrapper.

31
Q

What are Hooks in React?

A

They are essentially functions that open up new opportunities, such as:

  • Allows the removal of a lot of class components that we only used because we couldn’t have, e.g., a local state or ref, so the code for a component looks easier to read.
  • Enables you to use less code for the same effect.
  • Makes functions way easier to think about and test, e.g., by using the react-testing-library.
  • Can also take parameters, and the result of one can be easily be used by another hook (e.g., setState from useState in useEffect).
  • Minifies way better than classes, which tend to be a bit more problematic for minifiers.
  • Might remove HOCs and render props patterns in your app which introduced new problems despite having been designed to solve others.
  • Capable of being custom-built by any skilled React developer.
32
Q

name 3 basic hooks included as default in the updated React

A

useState, useEffect, and useContext

33
Q

Name 2 tools to help improve react code performance

A

Chrome Performance Tab:
https://developers.google.com/web/tools/chrome-devtools/evaluate-performance/reference

DevTools Profiler:https://reactjs.org/blog/2018/09/10/introducing-the-react-profiler.html

34
Q

Describe current state of react.

A

React has such a strong standing, backed by a large community following, that it will be difficult to dethrone. The React Community is great, it isn’t running out of ideas, and the core team is constantly working to improve React, adding new features and fixing old issues. React is also backed by a huge company, but the licensing problems are gone—it is MIT licensed right now.
ReactNative has been improved!