Interview questions react Flashcards

1
Q

What is React?

A

React is a front-end library developed by facebook. It allows for us to create really nice, reusable front-end UI components.

It can be used to develop complex/interactive Web and Mobile User Interfaces. It has a relatively massive amount of support.

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

What are some of the features of React?

A
  • Uses virtual DOM instead of the actual DOM.
  • Uses server-side rendering
  • follows uni-directional flow for data binding
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the limitations of React?

A
  • It’s just a library, not a full framework.
  • Its library is large and takes time to understand.
  • It can be difficult for novice programmers.
  • It can be complex with inline templating and JSX
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some advantages of React?

A
  • Increases application performance.
  • Can be used on both client and server side.
  • JSX increases code readability.
  • Easy to integrate with other frameworks.
  • Writing UI test cases is easy.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is JSX?

A

JavaScript XML. Utilizes JavaScript expressiveness with HTML-like template syntax.

-Becomes really easy to understand

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

What is the virtual DOM? How does it work?

A

It is a lightweight JavaScript object which is just a copy of the real DOM. It treats the elements, attributes, and contents as Objects as their properties.

-The render function creates a node tree out of the React components. It updates the tree in response to mutations in data model.

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

What is the difference between the real and virtual DOM?

A

Real:

  • Updates slow.
  • Can directly update HTML
  • Creates a new DOM if element updates.
  • DOM manipulation is expensive.
  • Too much memory wasted.

Virtual:

  • Updates fast
  • Cannot directly update HTML
  • Updates JSX if element updates.
  • DOM manipulation is easy.
  • No memory waste.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Why can’t browsers read JSX?

A

Browsers can read JavaScript objects, but JSX is not a regular JavaScript object.

  • To enable the browser to read JSX, we need to transform a JSX file to a JavaScript object using a transformer like Babel.
  • After transforming, pass it to the browser.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How different is React’s ES6 syntax when compared to ES5?

A

1: Use import instead of require.
2: Use export instead of module.exports.
3: Extend component instead of using function to render a component.
4: props change.
5: State declared in constructor instead of getInitialState.

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

How is React different from angular?

A

React:

  • Only the view of MVC
  • Server side rendering
  • Uses virtual DOM
  • One-way data binding
  • Compile time debugging
  • Made by facebook

Angular:

  • Complete MVC
  • Client side rendering
  • Uses real DOM
  • Two-way data binding
  • Run time debugging
  • Made by Google
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do you understand from “In React, everything is a component.”

A

Components are the building blocks of a React application’s UI. These components split up the entire UI into small independent and reusable pieces. Then it renders each of these components independent of each other without affecting the rest of the UI.

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

Explain the purpose of render() in React.

A

Each React component must have a render() mandatorily. It returns a single React element which is the representation of the native DOM component. If more than one HTML element needs to be rendered, then they must be grouped together inside one enclosing tag such as , ,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.</div>

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

How can you embed two or more components into one?

A

Place them both inside a form, group, div tag, etc.

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

What are props?

A

Props are shorthand for ‘properties’ in react. They are read-only and are immutable. Must be passed down from parent to child. Child CANNOT send a prop back to a parent.

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

What is state?

A

Are the source of data
Must be kept simple.
They are the objects which determine component rendering and behavior.
They are mutable, and can be used to create dynamic and interactive components.

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

Difference between state and props?

A

The parent component does not change the state value, while it does for props.

State changes inside of the actual component, while props change inside of the child components.

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

How can you update the state of a component?

A

this.setState();

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

What is the arrow function in React? How is it used?

A

They allow functions to bind the context of the components properly since in ES6 auto binding is not available by default.

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

Difference between stateful and stateless component?

A

Stateful:

  • Stores info about state change in memory.
  • Has authority to change state
  • Contains knowledge of past, present, and future changes in state.
  • Notified by stateless components about the requirement of the state change, and then send props down.

Stateless:

  • Calculates internal state of components.
  • Does not have authority to change state.
  • Contains no knowledge of past, present, and future state changes.
  • Receives props from stateful components and treat them as callbacks.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the three phases of React’s component’s lifecycle?

A

1: Initial Rendering Phase:
- Component is about to start its life journey and make its way to the DOM.

2: Updating Phase:
- Component is added to DOM, and it can get updated or re-rendered only when a prop or state change occurs.

3: Unmounting Phase:
- Component is destroyed and removed from DOM.

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

Explain the React lifecycle methods in detail

A

1: componentWillMount()
- Executed just before rendering takes place both on the client as well as server-side.

2: componentDidMount()
- Executed on the client side only after the first render.

3: componentWillReceiveProps() - Invoked as soon as the props are received from the parent class and before another render is called.
4: shouldComponentUpdate() - Returns true or false value based on certain conditions. If you want the component to update, return true else return false. By default, it returns false.
5: componentWillUpdate() - Called just before rendering takes place in the DOM.
6: componentDidUpdate() - Called immediately after rendering takes place.
7: componentWillUnmount() - called after the component is unmounted from the DOM. It is used to clear up the memory spaces.

22
Q

What is an event in React?

A
  • Events are the triggered reactions to specific actions like mouse hover, click, key press, etc.

Slight syntactical differences from DOM elements:

  • Events are named using camel case.
  • Events are passed as functions
23
Q

How do you create an event in React?

A

render the element with the event property calling the function.

24
Q

What are synthetic events in React?

A

Synthetic events are the objects which act as a cross-browser wrapper around the browser’s native event. They combine the behavior of different browsers into one API. This is done to make sure that the events show consistent properties across different browsers.

25
Q

What do you understand by refs in React?

A

Refs is the short hand for References in React. It is an attribute which helps to store a reference to a particular React element or component, which will be returned by the components render configuration function. It is used to return references to a particular element or component returned by render(). They come in handy when we need DOM measurements or to add methods to the components.

26
Q

What are some cases where you should use Refs?

A
  • When you need to manage focus, select text or media playback.
  • To trigger imperative animations
  • Integrate with third-party DOM libraries
27
Q

How do you modularize code in React?

A

By using the export and import properties, you can write components separately in different files.

28
Q

How are forms created in React?

A

In React, the state is contained in the state property of the component, and is ONLY updated via setState. Elements cannot directly update their state.

29
Q

What do you know about controlled and uncontrolled components?

A

Controlled:

  • Do not maintain own state.
  • Data is controlled by parent component.
  • Take in current values through props and then notify changes via callbacks.

Uncontrolled:

  • Maintain own state
  • Data is controlled by DOM
  • Refs are used to get current values.
30
Q

What are Higher Order Components? (HOC)

A

HOC are custom components which wraps another component within it.

They ccan accept any dynamically provided child component, but won’t modify or copy any behavior from input components. Are ‘pure’.

31
Q

What can you do with HOC?

A
  • Code reuse, logic and bootstrap abstraction
  • Render High Jacking
  • State abstraction and manipulation
  • Props manipulation
32
Q

What are Pure Components?

A

Pure components are the simplest and fastest components which can be written. They can replace any component which one has a render(). These components enhance the simplicity of the code and performance of the application.

33
Q

What is the significance of keys in React?

A

Keys are used for identifying unique Virtual DOM Elements with their corresponding data driving the UI.
They help React to optimize the rendering by recycling all the existing elements in the DOM.
These keys must be a unique number or string, using which React just reorders the elements instead of re-rendering them.
-Leads to an increase in application performance.

34
Q

What were the major problems with MVC framework?

A
  • DOM manipulation was expensive.
  • Applications were slow and inefficient
  • Huge memory wastage
  • Because of circular dependencies, complicated model was created around models and views.
35
Q

Explain Flux

A

An architectural pattern which enforces the uni-directional data flow.

It controls derived data and enables communication between multiple components using a central Store which has authority for all data. Any update in data throughout the application must occur here only. Flux provides stability to the application and reduces run-time errors.

36
Q

What is Redux?

A

Redux is one of the hottest libraries for front end development in today’s marketplace. It is a predictable state container for JavaScript applications and is used for the entire applications state management. Applications developed with Redux are easy to test and can run in different environments showing consistent behavior.

37
Q

What are the three principles that Redux follows?

A

1: Single source of truth:
The state of the entire application is stored in an object/state tree within a single store. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.

2: State is read-only:
The only way to change the state is to trigger an action. An action is a plain JS object describing the change. Just like state is the minimal representation of data, the action is the minimal representation of the change to that data.

3: Changes are made with pure functions:

In order to specify how the state tree is transformed by actions, you need pure functions. Pure functions are those whose return value depend solely on the values of their arguments.

38
Q

What do you understand by “Single source of truth”?

A

Redux uses ‘Store’ for storing the application’s entire state at one place. So all the component’s state are stored in the Store and they receive updates from the Store itself. The single state tree makes it easier to keep track of changes over time and debug or inspect the application.

39
Q

List down the components of Redux.

A

Action - An object that describes what happened.
Reducer - It is a place to determine how the state will change.
Store - State/Object tree of the entire application is saved in the Store.

View - Simply displays the data provided by the Store.

40
Q

How does the data flow through Redux?

A

https://d1jnx9ba8s6j9r.cloudfront.net/blog/wp-content/uploads/2017/09/flowda-768x333.png

41
Q

How are Actions defined in Redux?

A

Actions in React must have a type property that indicates the type of ACTION being performed. They must be defined as a String constant and you can add more properties to it as well.

In Redux, actions are created using the functions called Action Creators.

42
Q

Explain the role of a Reducer.

A

Reducers are pure functions which specify how the application’s state changes in response to an ACTION. Reducers work by taking in the previous state and action, and then it returns a new state. It determines what sort of update needs to be done based on the type of the action, and then returns new values. It returns the previous state as it is, if no work needs to be done.

43
Q

What is the significance of Store in Redux?

A

A store is a JavaScript object which can hold the application’s state and provide a few helper methods to access the state, dispatch actions and register listeners.

The entire state/object tree of an application is saved in a single store. As a result of this, Redux is very simple and predictable. We can pass middleware to the store to handle processing of data as well as to keep a log of various actions that change the state of stores. All the actions return a new state via reducers.

44
Q

How is Redux different from Flux?

A

Flux:

1: The Store contains state and change logic.
2: There are multiple stores.
3: All the stores are connected and flat.
4: Has singleton dispatcher
5: React components subscribe to the store
6: State is mutable.

Redux:

1: Store and change logic are separate
2: There is only one store.
3: Single store with hierarchical reducers.
4: No concept of dispatcher.
5: Container components utilize connect.
6: State is immutable.

45
Q

What are the advantages of Redux?

A

Predictability of outcome - Since there is always one source of truth, there is no confusion about how to sync the current state with actions and other parts of the application.

Maintainability - The code becomes easier to maintain with a predictable outcome and strict structure.

Server side rendering - You just need to pass the store created on the server to the client side. This is very useful for initial rendering and provides a better user experience as it optimizes the application performance.

Developer tools - From actions to state changes, developers can track everything going on in the application in real time.

Community and ecosystem - Redux has a huge community behind it which makes it even more captivating to use. A large community of talented individuals contribute to the betterment of the library and develop various applications with it.

Ease of testing - Redux’s code is mostly functions which are small, pure, and isolated. This makes the code testable and independent.

Organization - Redux is precise about how code should be organized, this makes the code more consistent and easier when a team works with it.

46
Q

What is React Router?

A

React Router is a powerful routing library built on top of React, which helps in adding new screens and flows to the application.

This keeps the URL in sync with data that’s being displayed on the web page. It maintains a standardized structure and behavior and is used for developing single page web applications. React Router has a simple API.

47
Q

Why is switch keyword used in React Router v4?Refs is the short hand for References in React. It is an attribute which helps to store a reference to a particular React element or component, which will be returned by the components render configuration function. It is used to return references to a particular element or component returned by render(). They come in handy when we need DOM measurements or to add methods to the components.

A

Although a <div> is used to encapsulate multiple routes inside the Router, the ‘switch’ keyword is used when you want to display only a single route to be rendered amongst the several defined routes. The tag when in use matches the typed URL with the defined routes in sequential order. When the first match is found, it renders the specified route, thereby bypassing the remaining routes.</div>

48
Q

Why do we need a Router in React?

A

A Router is used to define multiple routes and when a user types a specific URL, if this URL matches the path of any ‘route’ defined inside the router, then the user is redirected to that particular route. We need to add a router library to our app that allows creating multiple routes with each leading to us a unique view.

49
Q

List the advantages of React Router

A

1: Just like how React is component-based, so is React-Router. A router can be visualized as a single root component in which we enclose specific child routes.
2: No need to manually set a history value. In React Router v4 all we need to do is wrap them within BrowserRouter.
3: The packages are split. Three packages one each for web, native, and core. It supports the compact size of our app. It’s easy to switch over based on similar coding style.

50
Q

How is React Router different from conventional routing?

A

Conventional:

  • Each view corresponds to a new file.
  • A HTTP request is sent to the server and corresponding HTML page is received.
  • User actually navigates across different pages for each view.

React

  • Only single HTML page is involved.
  • Only history attribute is changed.
  • User is duped into thinking they are navigating across different pages.