React Flashcards

1
Q

What is React?

A

React is JavaScript library, developed by Facebook for buildig user interface. It allows developer to create separate reusable components.

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

What is JSX?

A

JSX is a syntaxical sugar over React components. It allows you to write HTML-like code, and then it will be converted into React elements.

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

How does React handle DOM manipulation?

A

React creates a Virtual DOM tree, which is representation of real DOM in form of JavaScript objects tree, and each time after React state changed, React compares old Virtual DOM and new Virtual DOM, and updates real DOM only in places where we have differences between new and old versions of Virtual DOM trees.

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

What is state in React?

A

State in React is an information container, where we can store information about current component data. We can create new state variable using React useState hook.This case we get an array, where first item is current state variable, and second is a function, using what we can change this variable value. When we change this variable value, component will be re-rendered.

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

What is property in React?

A

Property in React is readonly external value, which we can get inside component from its parent component as some kind of parameter. Any value, what we provide as attribute inside JSX component tag, will become property and will be accessible inside this one component props object.

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

What are some of the limitations of React?

A

React is a view-only library, which mean, that we can have certain problems with other aspects of an application, like styles management, or state management. And to solve these problems, we need to use another libraries which provide required functionality. For example, in default React all styles have global scope. So, if you want to have scoped styles, like you have in Vue, for example, you need to use additional instruments, which will do this for you. For example, in React framework Next.js we have inbuilt CSS modules support. So, with it, this is possible to make styles scoped.

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

What are React hooks?

A

React hooks are special functions which you can call inside your React functional components to get access to certain things, which you usually have in other frontend frameworks.

These features, for example, include state management, component lifecycle hooks, memoization and some other React-specific things.

For example, using useState hook, you can create a component-specific variable, which value will be acessible after this component will re-render.
Using useEffect hook, you can do certain actions when certain other variables or properties were updated.
Using useMemo, you can memoize certain information until this hook dependencies will be updated.

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

How do you handle errors in React?

A

In case if we want to catch error inside synchronous code of separate component, we can use regular try / catch block.

If we are talking about errors, what happened inside React internal mechanisms on render stage, then we need to use class component componentDidCatch method.

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

What are synthetic events in React?

A

Synthetic events are just regular events, wrapped with certain React-specific classes. They provide cross-browser interface for events.

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

In React, why do we need to use keys in lists?

A

Because React uses key property as unique identifier for elements. Using it, React can track components, and always exactly understand, what elements should be added, removed, or updated.

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

What are the rules for using hooks in React?

A

First of all, on every render must be called same number of hooks. So, this is highly not recommended to call hooks inside conditions or loops, because this case number of hooks will change from render to render.

That is also the reason why this is allowed to use hooks only inside functional components and other hooks, and not inside regular functions.

Finally, custom hooks names must start from “use” word.

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

What are lifecycle stages of React component?

A

There are three stages of lifecycle of React functional component.

First is mounting. On this stage component state is initialized, and all useEffect hooks are called once.

Second is updation. On this stage, are called all useEffects cleanup callbacks, then goes render, and finally, after render, are called all useEffects where any of their dependencies has changes.

Finally, unmounting. On this stage are called all useEffects cleanup callbacks, and then component is destroyed.

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

What is difference between useCallback and useMemo? And how they are usually used?

A

The difference is in how they work. useCallback takes a callback function as first argument, and array of dependencies as second argument, and returns one and same instance of this function until its dependencies are changed.

useMemo just returns result of provided callback execution, and recalculates it each time when its dependencies are updated.

Typically, useMemo is used when we need to optimize some calculations, which we don’t want to be executed on each render.

useCallback is used to keep one and same function instance between re-renders, which is useful if we use React components created using memo wrapper, or when we provide this function as one of items in dependencies array of other hooks.

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

What is reconciliation?

A

Reconciliation in React is a process of comparing old and new Virtual DOM trees, understanding of what should be updated, and then updating real DOM tree.

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

What is Flux architecture?

A

Flux architecture is a architecture of monodirectional data flow inside application.

It consist of few parts, which are View, Actions, Dispatcher and State.

Store contains information about current application state and some handlers, which handle state updation.
View can read State, and use it to show some information, but can’t write into it.
Actions can be called by View in order to interact with Store.
Actions does not mutate State directly, they just create requests and send them to Dispatcher.
Dispatcher receives information about necessary changes in Store, and generates event, which is sent to all connected Stores.
Finally, stores listen to events, and update internal state inside event handlers.

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

What is the difference between client side and server side rendering?

A

In case of CSR client receives almost empty HTML file and a lot of JavaScript code.
Then, client executes this code in order to build the application instance and generates the rest of HTML using JavaScript.

In case of Server – Side rendering, there are two ways we can go.

First is Server Side Generation. In this approach, we have pre-generated list of pages with static information, which we send to client. But this case client anyway has to get all dynamic information using API.

Second is Server Side Rendering. In this approach, we do more job on server side. After page was requested, we can make some API requests on server side, and then use this data to render more advanced version of web page.

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

What are pure components in React?

A

Pure components in React are special kind of components, which are updated only when their props are updated. So, their re-render can be triggered only by their props updating. In functional components we can create pure component using React.memo wrapper.

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

What is Lifting State Up in React?

A

Lifting state up is a development method, when in case if we have multiple components which need access to same changing data, then we move this state to their nearest common ancestor.

This helps to control this state in more centralized way, and simplifies one component updation when another component changes this state.

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

What are Higher-Order Components?

A

Higher order components are components which take another components render functions as properties and use these functions to render these components inside itself.

We can use higher-order components for multiple aims. Simpliest example is React memo component, which turns component into pure component, which is updated only when its props are updated.

Also, we can use this functionality to create slots, like in Vue. So, you provide content for this component in form of another component.

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

What are fragments?

A

Fragment is a special React component which you can use to group certain list of components without creating container using HTML elements, like div or section.

This makes components management more convenient, because this case you don’t create any additional DOM components, and will have less problems with CSS selectors, for example.

Also, this is possible to use key attribute on fragment component.

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

What are portals?

A

Portals in React is a way using what you can render certain component outside of its default place in components tree.

To do this you need to call ReactDOM.createPortal function, where you provide component itself and DOM node, which will become parent of this component.

21
Q

What is the typical use case of portals?

A

For example, when we want to create a modal window or tooltip. These components should be shown above any other items, but sometimes this is not possible because of existing CSS styles of elements above in tree. So this is necessary to move component body outside or regular tree hierarchy.

22
Q

What are stateful and stateless components?

A

If behaviour of component depends on its internal state, then this is stateful component.

If component doesn’t have any internal state then its stateless component.

23
Q

What are advantages and disadvantages of React?

A
  1. Can be used both on client and server side.
  2. Quite agile, because this is just a view library and doesn’t make a lot of restrictions, so there are no big problem to integrate other instruments into application created using React.
  3. Gives wide control over state watching and memoization.
24
Q

What react-dom package is used for?

A

react-dom package provides DOM-specific methods, which can be used to have better control over react components inside DOM tree.

For example, these are render or portal.

25
Q

How to use innerHTML in React?

A

You can use dangerouslySetInnerHTML attribute of React component to do this.

26
Q

What is difference between events handling in React and HTML?

A
  1. Events in React are named in camel case, but in HTML they are in lowercase
  2. Events in React are passed as functions, but in HTML they are just text.
27
Q

What is switching component?

A

Switching component is a component what renders one of many components.

28
Q

Why component names should start from capital letter?

A

This is convention, because only HTML elements and SVG tags start from lowercase letter. But all JSX components should start with upper case letter.

29
Q

How to use React label element?

A

If you use label element in React, you should use htmlFor attribute instead, because some default words are reserved by JavaScript, and JSX can’t use them.

30
Q

How we can find the version of React during runtime inside browser?

A

Just access React.version property.

31
Q

What types of React project files grouping exist?

A
  1. Grouping by features or routes.
  2. Grouping by file type.
32
Q

What is AJAX?

A

AJAX is asynchronous JavaScript and XML. In case of AJAX we don’t update whole browser page when we need to get new information from the server, instead, we make server request, get some data through its API, and then dynamically update page using JavaScript.

33
Q

What are render props?

A

Render props are props which take component render function and then can use this function to render this component.

34
Q

What is React Router and how to use it?

A

React router is a special mechanism in React, which allows you to switch between different pages on different URLs in predictable and configurable manner.

You can use Router and Route components, or just use createBrowserRouter function to create new instance of browser router with predefined paths, and then use it inside RouterProvider component.

35
Q

How does Redux work and what are its fundamental principles?

A

Redux is a predictable state container which is possible to use with any JavaScript application.

In has three fundamental principles:
1. Single source of truth. All state of application is stored inside an object tree inside single store. This single tree makes it easier to track changes and debug application.
2. State is read-only. When you need to make changes to state, you use dispatcher to provide information about changes, and then these changes are applied and saved into new state object.
3. Changes are made using pure functions, which take old state as input and return new state as output.

36
Q

React advantages and disadvantages comparing to Vue.

A

Advantages:
1. React is more widespread, so its community is bigger. This leads to better support and more third-party libraries and components that you can use.
2. React is more agile. While in Vue you build your application around main framework structure, React is just a view library and gives you higher control.
3. You can use React to write views for mobile devices too, using React Native.

Disadvantages:
React is harder to learn. Vue is quite simple, and gives you predictable and intuitive approach for application development. Same time, you have many non-intuitive underwater obstacles in React, so you have to spend more time on learning them.

37
Q

What is rendering hijacking in React?

A

Rendering hijaking is an ability to control what a component will output from another component. So, you wrap your component into higher-order component, and depending on props or state changes, you can change logic of component rendering.

38
Q

Do I need to keep all my state into Redux? Should I ever use react internal state?

A

App logic related infromation should be stored inside Redux, but simple internal state like value of input element, for example, can stay inside component.

39
Q

What is React.lazy function and how you can use it?

A

React.lazy is a method which allows you to import certain component dynamically, only when it is needed. This can be used for chunking your application bundle into smaller parts and load only needed.

40
Q

What are differences between Flux and Redux

A

Flux is just architectural approach for designing data flow inside application, but Redux is a library, which almost fully implements Flux approach.

The biggest difference between Flux and Redux is that Redux does not allow to mutate existing state. When you need to perform mutation, you have to create new version of existing state.

41
Q

What are the conditions to safely use the index as a key?

A

If elements of list don’t change.

42
Q

What are loadable components?

A

Similar to React.lazy, but for server-side rendering.

43
Q

What is suspense component?

A

Suspense component is some temporary overlay component, which shows some content instead of dynamically loaded component, during its loading period.

44
Q

What is route based code splitting?

A

This is code splitting into separate dynamically loaded chunks, where separation criteria is a certain route. So, for example, if we have few separate pages, we can make them lazy-loaded, and through this get separate bundles for each of these pages.

45
Q

What are context consumer and provider?

A

Provider is a component which makes certain contex accesible for its children component.

Consumer is a component, which is receiving context from its parent context provider component.

46
Q

When do you need to use refs?

A

When you need to somehow interact with DOM tree element. This can be just needed to provide it to some other functions, like ReactDOM.portal, or in order to imperatively read or write some properties of these elements.

47
Q

Is ref argument available for all functions or class components?

A

By default, not. If you by some reason need to access ref of component, you have to wrap them with forwardRef.

48
Q

How do you set default value for uncontrolled component?

A

Using defaultValue attribute

49
Q

What is strict mode in React?

A

StrictMode is a special wrapper component, which adds some additional checks for its subtree for debugging purposes.

50
Q

What is the benefit of strict mode?

A

It will notify you about legacy functionality use and possible side effects during render phase.