React Junior Flashcards

1
Q

How does React work?

A

React creates a virtual DOM. When state changes in a component it firstly runs a “diffing” algorithm, which identifies what has changed in the virtual DOM. The second step is reconciliation, where it updates the DOM with the results of diff.

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

What is context?

A

Context provides a way to pass data through the component tree without having to pass props down manually at every level. For example, authenticated user, locale preference, UI theme need to be accessed in the application by many components.

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

What is virtual DOM?

A

The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

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

What is props?

A
  • input data passed to components similar to html attributes
  • Trigger state changes.
  • Use via this.props.reactProp inside component’s render() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the use of refs?

A

Allow us to directly access a DOM element

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

What’s the difference between a controlled component and an uncontrolled one in React?

A

A controlled component has its state completely driven by React,
Uncontrolled components can maintain their own internal state. E.g., a textarea’s value.

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

What are controlled components?

A

A ReactJS component that controls the input elements within the forms on subsequent user input is called “Controlled component”. i.e, every state mutation will have an associated handler function.

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

What is state in ReactJS?

A

State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.

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

When to use a Class Component over a Functional Component?

A

If the component need state or lifecycle methods then use class component otherwise use functional component, we basically should stop using them in favor of Hooks according to React official documentation.

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

What does it mean for a component to be mounted in React?

A

It has a corresponding element created in the DOM and is connected to that.

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

What are fragments?

A

It’s common pattern in React which is used for a component to return multiple elements. Fragments let you group a list of children without adding extra nodes to the DOM.

<>

<div></div>

<div></div>

>

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

What is JEST?

A

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It’s often used for testing React components.

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

What are the advantages of ReactJS?

A
  • Increases the application’s performance with Virtual DOM
  • JSX makes code is easy to read and write
  • It renders both on client and server side
  • Easy to integrate with other frameworks (Angular, BackboneJS) since it is only a view library
  • Easy to write UI Test cases and integration with tools such as JEST.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is ReactJS?

A

ReactJS is an open-source frontend JavaScript library which is used for building user interfaces specifically for single page applications.

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

How to write comments in ReactJS?

A

{/* Single-line comments */}

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

How would you write an inline style in React?

A

<div></div>

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

What are the major features of ReactJS?

A
  • Uses VirtualDOM instead RealDOM considering that RealDOM manipulations are expensive
  • Supports server-side rendering
  • Follows Unidirectional data flow or data binding
  • Uses reusable/composable UI components to develop the view
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the differences between a class component and functional component?

A

Basically only difference is that component uses lifecycle hooks and local state. But with react hooks for functional components basically no difference.

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

What are the advantages of using React?

A
  • easy to know how a component is rendered, you just need to look at the render function.
  • JSX makes it easy to read the code of your components. It is also really easy to see the layout, or how components are plugged/combined with each other.
  • You can render React on the server-side. This enables improves SEO and performance.
    It is easy to test.

-You can use React with any framework (Backbone.js, Angular.js) as it is only a view layer.

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

What is virtual DOM?

A

The virtual DOM (VDOM) is an in-memory representation of Real DOM. The representation of a UI is kept in memory and synced with the “real” DOM. It’s a step that happens between the render function being called and the displaying of elements on the screen. This entire process is called reconciliation.

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

What is props?

A
  • input data passed to components similar to html attributes
  • Trigger state changes.
  • Use via this.props.reactProp inside component’s render() method.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the use of refs?

A

Allow us to directly access a DOM element

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

What’s the difference between a controlled component and an uncontrolled one in React?

A

A controlled component has its state completely driven by React,
Uncontrolled components can maintain their own internal state. E.g., a textarea’s value.

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

What are controlled components?

A

A ReactJS component that controls the input elements within the forms on subsequent user input is called “Controlled component”. i.e, every state mutation will have an associated handler function.

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

What is state in ReactJS?

A

State of a component is an object that holds some information that may change over the lifetime of the component. We should always try to make our state as simple as possible and minimize the number of stateful components.

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

When to use a Class Component over a Functional Component?

A

If the component need state or lifecycle methods then use class component otherwise use functional component, we basically should stop using them in favor of Hooks according to React official documentation.

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

What does it mean for a component to be mounted in React?

A

It has a corresponding element created in the DOM and is connected to that.

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

What is JEST?

A

Jest is a JavaScript unit testing framework made by Facebook based on Jasmine and provides automated mock creation and a jsdom environment. It’s often used for testing React components.

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

What are the advantages of ReactJS?

A
  • Increases the application’s performance with Virtual DOM
  • JSX makes code is easy to read and write
  • It renders both on client and server side
  • Easy to integrate with other frameworks (Angular, BackboneJS) since it is only a view library
  • Easy to write UI Test cases and integration with tools such as JEST.
30
Q

What is ReactJS?

A

ReactJS is an open-source frontend JavaScript library which is used for building user interfaces specifically for single page applications.

31
Q

How to write comments in ReactJS?

A

{/* Single-line comments */}

32
Q

How would you write an inline style in React?

A

<div></div>

33
Q

What are the major features of ReactJS?

A
  • Uses VirtualDOM instead RealDOM considering that RealDOM manipulations are expensive
  • Supports server-side rendering
  • Follows Unidirectional data flow or data binding
  • Uses reusable/composable UI components to develop the view
34
Q

What are the differences between a class component and functional component?

A

Basically only difference is that component uses lifecycle hooks and local state. But with react hooks for functional components basically no difference.

35
Q

What are the advantages of using React?

A
  • easy to know how a component is rendered, you just need to look at the render function.
  • JSX makes it easy to read the code of your components. It is also really easy to see the layout, or how components are plugged/combined with each other.
  • You can render React on the server-side. This enables improves SEO and performance.
    It is easy to test.

-You can use React with any framework (Backbone.js, Angular.js) as it is only a view layer.

36
Q

Where in a React component should you make an AJAX request?

A

componentDidMount is where an AJAX request should be made in a React component.

The method is only executed once during the component’s life. Importantly, you can’t guarantee the AJAX request will have resolved before the component mounts. If it doesn’t, that would mean that you’d be trying to setState on an unmounted component, which would not work. Making your AJAX request in componentDidMount will guarantee that there’s a component to update.

37
Q

What is the difference between state and props?

A
  • State is a data structure where default values can be changed
  • Props is read-only, cannot be altered, may be functions as well as data
38
Q

What is the difference between a Presentational component and a Container component?

A
  • Presentational components are concerned with how things look. They generally receive data and callbacks exclusively via props. These components rarely have their own state, but when they do it generally concerns UI state, as opposed to data state.
  • Container components are more concerned with how things work. These components provide the data and behavior to presentational or other container components. They call Flux actions and provide these as callbacks to the presentational components. They are also often stateful as they serve as data sources.
39
Q

When rendering a list what is a key and what is it’s purpose?

A

Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity. The best way to pick a key is to use a string that uniquely identifies a list item among its siblings. Most often you would use IDs from your data as keys.

40
Q

How to create refs?

A

Refs are created using React.createRef() method and attached to React elements via the ref attribute. In order to use refs throughout the component, just assign the ref to the instance property with in constructor. We can also use it in functional components with the help of closures.

<div></div>

<div></div>

41
Q

What happens when you call setState?

A

The state property is updated in a React component with the object passed into setState, and this is done asynchronously. It tells React that this component and its children need to be re-rendered, but React may not do this immediately (it may batch these state update requests for better performance).

42
Q

What are stateful components?

A

A component whose state gets initialized in the constructor.

43
Q

How would you prevent a component from rendering in React?

A

Return null from the render method.

44
Q

What is JSX?

A

JavaScript XML - an extension that allows us to combine certain aspects of HTML and JavaScript, compiles into JavaScript

45
Q

How error boundaries handled in React (15)?

A

React15 provided very basic support for error boundaries using the method name unstable_handleError. Later this has been renamed as componentDidCatch starting from React16 beta release.

46
Q

Where is the state kept in a React + Redux application?

A

In the store.

47
Q

What are the limitations of ReactJS?

A
  • React is just a view library, not a full-blown framework
  • There is a learning curve for beginners who are new
    to web development.
  • Integrating React.js into a traditional MVC framework
    requires some additional configuration
  • The code complexity increases with inline templating and JSX.
  • Too many smaller components leading to over-engineering or boilerplate
48
Q

What is the difference between React Native and React?

A

ReactJS - a JavaScript library, supporting both front end web and being run on the server, for building user interfaces and web applications.

React Native - a mobile framework that compiles to native app components, allowing you to build native mobile applications (iOS, Android, and Windows) in JavaScript that allows you to use ReactJS to build your components, and implements ReactJS under the hood.

49
Q

What are stateless components?

A

If the behaviour is independent of its state then it can be a stateless component.

50
Q

How is React different from AngularJS (1.x)?

A

For example, AngularJS (1.x) approaches building an application by extending HTML markup and injecting various constructs (e.g. Directives, Controllers, Services) at runtime. As a result, AngularJS is very opinionated about the greater architecture of your application — these abstractions are certainly useful in some cases, but they come at the cost of flexibility.

By contrast, React focuses exclusively on the creation of components, and has few (if any) opinions about an application’s architecture. This allows a developer an incredible amount of flexibility in choosing the architecture they deem “best” — though it also places the responsibility of choosing (or building) those parts on the developer.

51
Q

What is the point of Redux?

A

Application state management that is easy to reason about, maintain and manage in an asynchronous web application environment.

52
Q

Why is it necessary to capitalize the components?

A

It is necessary because components are not the DOM element but they are constructors. If they are not capitalized, they can cause various issues and can confuse developers with several elements.

53
Q

What is Flow?

A

Flow is a static type checker, designed to find type errors in JavaScript programs. It helps you catch errors involving null, unlike most type systems.

54
Q

What types of components are there?

A

Functional or Class

55
Q

What is the purpose of callback function as an argument of setState?

A

The callback function is invoked when setState finished and the component gets rendered. Since setState is asynchronous the callback function is used for any post action.

setState({name: ‘sudheer’}, () => console.log(‘The name has updated and component re-rendered’));

Note: It is recommended to use lifecycle method rather this callback function.

56
Q

What are portals in ReactJS?

A

Portal is a recommended way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.

57
Q

How to pass a parameter to an event handler or callback?

A

You can use an arrow function to wrap around an event handler and pass parameters:

this.handleClick(id)} />
This is equivalent to calling .bind as below,

58
Q

What’s the difference between an “Element” and a “Component” in React?

A

A React element describes what you want to see on the screen. Not so simply put, a React element is an object representation of some UI.

A React component is a function or a class which optionally accepts input and returns a React element (typically via JSX which gets transpiled to a createElement invocation).

59
Q

What happens at the highest level during the lifecycle of a React component?

A
  • Initialization
  • State/Property Updates
  • Destruction
60
Q

What is Flux?

A

Unidrectional application flow paradigm popular a few years back in React; mostly superceded by Redux these days.

61
Q

What is the difference between component and container in react Redux?

A

Component is part of the React API. A Component is a class or function that describes part of a React UI. Container is an informal term for a React component that is connected to a redux store. Containers receive Redux state updates and dispatch actions, and they usually don’t render DOM elements; they delegate rendering to presentational child components.

62
Q

What is inline conditional expressions?

A
  • if statements
  • ternary operators
  • &&
63
Q

How do you prevent the default behavior in an event callback in React?

A

You call e.preventDefault(); on the event e passed into the callback.

64
Q

What is reconciliation?

A

When a component’s props or state change, React decides whether an actual DOM update is necessary by comparing the newly returned element with the previously rendered one. When they are not equal, React will update the DOM. This process is called reconciliation.

65
Q

What is the purpose of using super constructor with props argument?

A

A child class constructor cannot make use of this reference until super() method has been called. The same applies for ES6 sub-classes as well. The main reason of passing props parameter to super() call is to access this.props in your child constructors.

66
Q

What happens when you call “setState”?

A

The first thing React will do when setState is called is merge the object you passed into setState into the current state of the component. This will kick off a process called reconciliation which will update the UI based on this new state.

67
Q

Describe how events are handled in React.

A

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser’s native event. It has the same interface as the browser’s native event. React doesn’t actually attach events to the child nodes themselves. React will listen to all events at the top level using a single event listener.

68
Q

What is the difference between Element and Component?

A

An element is a plain object describing what you want to appear on the screen in terms of the DOM nodes or other components. Elements can contain other elements in their props. Creating a React element is cheap. Once an element is created, it is never mutated.

A component takes props as an input, and returns an element tree as the output. JSX transpiled as createElement at the end.

69
Q

What are Higher-Order components?

A

A higher-order component (HOC) is a function that takes a component and returns a new component.

70
Q

How do we pass a property from a parent component props to a child component?

A