React Flashcards

1
Q

What does webpack do in React? What files does webpack affect in React?

A

Webpack is a static module bundler. It goes through your src folder and creates a new package which consists of the very bare minimum number of files required, often just a single bundle.js file which can be plugged in to the html file easily and used for the application.

Creating a huge monolithic JavaScript file is ugly and difficult to maintain, but multiple JavaScript files require multiple requests to fetch, and can add significant overhead. The solution is to write code splitting it into as many files as you need, and using require() or import statements to connect them together as we see fit. When we require something from a file, that becomes a dependency of that file. All our interconnected files and assets form a dependency graph. Then we use a bundler to to parse through these files and connect them appropriately based on the require and import statements and perform some more optimizations on top of it to generate one (sometimes more than one) JavaScript files which we can then use.

Webpack gives you control over how to treat different assets it encounters. For example, you can decide to inline assets to your JavaScript bundles to avoid requests. Webpack also allows you to use techniques like CSS Modules to couple styling with components, and to avoid issues of standard CSS styling. This flexibility is what makes webpack so valuable.

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

What is JSX?

A

JSX is a syntax extension to JavaScript that produces React elements. React embraces the fact that rendering logic is inherently coupled with other UI logic: how events are handled, how the state changes over time, and how the data is prepared for display.

JSX is more declarative and reduces overall code complexity.
You can put any valid JavaScript expression inside the curly braces in JSX.

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

What is the root DOM node?

A

Applications built with just React usually have a single root DOM node and everything inside it will be managed by React DOM.

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

What is a react element?

A

It is a plain JavaScript Object. We are writing a HTML like element which gets converted to a JavaScript Object.

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

What is the virtual DOM?

A

The Virtual DOM is an abstraction of the HTML DOM. An in-memory copy of your original DOM that React updates directly. Manipulating the virtual DOM is easier and faster because nothing is changed in the UI.
Normally to update the DOM, you have to traverse through and re-render which can be very costly.
-First, ReactJS creates a copy of the original DOM, calling it the Virtual DOM. Each of the nodes of the Virtual DOM represents an element.
-Next, if there is a state update of an element, a new Virtual DOM is created.
-The diffing algorithm identifies the difference of the change. In this case, a subtree from the changed node has been identified as the diff.
-Last, the ReactJS runs a batch update to update the Original DOM with these changes to keep it in sync.

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

What is the reconciliation process?

A

React compares the virtual DOM with a version before the update in order to know what changed. This way, only the element that changed are updated in the real DOM.

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

Can you modify props?

A

Whether you declare a component as a function or a class, it must never modify its own props. All React components must act like pure functions with respect to their props.

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

Why should you not modify state directly? i.e. this.state….= value

A

When you modify the state directly, it doesn’t create a new state object (unlike setState). React won’t be able to see that there is a change of state.

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

What are components and what are it’s advantages?

A

React uses component-based architecture for developing applications. Components are independent and reusable bits of code. These components can be shared across various applications having similar functionality. The re-use of components increases the pace of development.

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

What is React?

A

React is a front-end and open-source JavaScript library which is useful in developing user interfaces specifically for applications with a single page. It is helpful in building complex and reusable user interface(UI) components of mobile and web applications as it follows the component-based approach.

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

What are the advantages of using React?

A
  • Use of Virtual DOM to improve efficiency
  • SEO friendly (due to server-side rendering)
  • Reusable components
  • Huge ecosystem of libraries to choose from
  • Developer Tools
  • One way data flow
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the disadvantages of using React?

A
  • JSX may be difficult to get used to
  • Development pace is very fast and can be difficult to keep up with
  • Documentation has a hard time keeping up with pace of development
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is useState() in React?

A

The useState() is a built-in React Hook that allows you for having state variables in functional components. It should be used when the DOM has something that is dynamically manipulating/controlling. It returns a pair of values: the current state and a function that updates it.

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

What are hooks and why did react create them?

A

Hooks allow you to reuse stateful logic without changing your component hierarchy. This makes it easy to share Hooks among many components or with the community. Hooks give you the ability to create your own abstractions that do not inflate your react component tree and create wrapper hell.

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

Downsides of classes

A
  • Hard for humans
  • Hard for machines… in minified component file, all method names are still unminified
  • Difficult to implement hot-reloading reliably
  • Large components with logic split across different lifecycles
  • Wrapper hell
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the rules with hooks?

A

React relies on the order of the hook calls. Therefore, the hook cannot be called inside a condition. Otherwise the order could change.

17
Q

How do you work with side effects in a class? Functional component?

A

You need to use the componentDidUpdate lifecycle method in a class and useEffect in a functional component.

18
Q

How do you clean up/unsubscribe after a useEffect? (similar to a componentWillUnmount)

A

Return a function. So for example if you added an event listener, you would return a remove event listener.

19
Q

What is a ref

A

It persists values between renders of your component. Unlike state, a ref does not cause your component to rerender when it gets changed. This can be very useful! useRef returns an object with a property of current.
Also useful for accessing DOM elements.

20
Q

How do you deal with an event handler that needs an argument passed through?

A

It needs to be a callback function or else it’ll run the moment the component renders.
Ex.
Good: onClick={clickHandler}
Bad: onClick={clickHandler(prop)}

Correction: onClick={() => clickHandler(prop)}

21
Q

What is children prop?

A

Children prop is used to access the data put between opening & closing tag of component.

22
Q

Why react uses className instead of class attribute?

A

Class is a reserved keyword in javascript that is why to react uses className instead of class.

23
Q

What is a higher order component in React?

A

A higher-order component is a component that takes a component and returns a new component.

24
Q

Where should we make an ajax call to fetch the data from API and render in the component (using class-based components)?

A

We should make AJAX call on componentDidMount() lifecycle method. This method gets executed when the component loads into the DOM first time. In this way, we can have our data displayed in the render method.

25
Q

Explain the use of Babel in React

A

Babel is a transpiler that compiles JSX code into regular javascript.

26
Q

What are PropTypes in React?

A

PropTypes help React identify the types of props passed to the component.

27
Q

What is ReactDOM?

A

ReactDOM is a top-level React API to render a react element into the DOM with the help of the ReactDOM.render() method.

28
Q

What is React Context?

A

React Context is an API that allows to pass data down through a tree of components without having to pass props to each component in the tree.

29
Q

How does React prevent injection attacks?

A

React DOM escapes any values embedded in JSX before rendering them into the DOM

30
Q

What does component mounting means in React?

A

Component Mounting means the corresponding element has been created into the DOM.

31
Q

Explain error boundaries in React?

A

Error boundary helps to log errors and display a fallback UI in case of an error. Error boundary component implements getDerivedStateFromError & componentDidCatch methods.

32
Q

What is the use of empty tags <> > ?

A

Empty tags are used to create Fragments in React.