React Interview questions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Real: updates slow, can directly update HTML, creates new DOM element updates, DOM manipulation is expensive, too much memory waste. Virtual: updates faster, can't directly update HTML, update the JSX if element updates, DOM manipulation is easy, no memory wast.

Differentiate between Real DOM and Virtual DOM

Props: Parent component can change value, Changes inside child components. State: Changes inside component

Differentiate between states and props.

Flux is 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.

Explain Flux.

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 <form>, <group>,<div> etc. This function must be kept pure i.e., it must return the same result each time it is invoked.

Explain the purpose of render() in React.

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. Below is an example of Action and Action Creator: function addTodo(text) { return { type: ADD_TODO, text } }

How are Actions defined in Redux?

State of a component can be updated using this.setState().

How can you update the state of a component?

We can modularize code by using the export and import properties. They help in writing the components separately in different files.

How do you modularize code in React?

React: Only the View of MVC, Server-side rendering, uses virtual DOM, One-way data binding, compile time debugging. Angular: Complete MVC, Client-side rendering, uses real DOM, Two-way data binding, runtime debugging

How is React different from Angular?

Redux is composed of the following components: Action - It's 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.

List down the components of Redux.

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

What are Pure Components?

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. **I need to look into this more

What are synthetic events in React?

It uses the virtual DOM instead of the real DOM. It uses server-side rendering. It follows uni-directional data flow or data binding.

What are the features of React?

Props is the shorthand for Properties in React. They are read-only components which must be kept pure i.e. immutable. They are always passed down from the parent to the child components throughout the application. A child component can never send a prop back to the parent component. This help in maintaining the unidirectional data flow and are generally used to render the dynamically generated data.

What is Props?

React is a front-end JavaScript library developed by Facebook It follows the component based approach which helps in building reusable UI components

What is React

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. So basically, we need to add a Router library to our app that allows creating multiple routes with each leading to us a unique view. <switch> <route exact path='/' component={Home}/> <route path='/posts/:id' component={Newpost}/> <route path='/posts' component={Post}/> </switch>

Why do we need a Router in React?

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 <switch> 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.

Why is switch keyword used in React Router v4?

Following are the cases when refs should be used: When you need to manage focus, select text or media playback To trigger imperative animations Integrate with third-party DOM libraries

26. List some of the cases when you should use Refs.

Code reuse, logic and bootstrap abstraction Render High jacking State abstraction and manipulation Props manipulation

31. What can you do with HOC?

componentWillMount() - Executed just before rendering takes place both on the client as well as server-side. componentDidMount() - Executed on the client side only after the first render. componentWillReceiveProps() - Invoked as soon as the props are received from the parent class and before another render is called. shouldComponentUpdate() - Returns true or false value based on certain conditions. If you want your component to update, return true else return false. By default, it returns false. componentWillUpdate() - Called just before rendering takes place in the DOM componentDidUpdate() - Called immediately after rendering takes place. componentWillUnmount() - Called after the component is unmounted from the DOM. It is used to clear up the memory spaces.

Explain the lifecycle methods of React components in detail.

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.

Explain the role of Reducer.

React forms are similar to HTML forms. But in React, the state is contained in the state property of the component and is only updated via setState(). Thus the elements can't directly update their state and their submission is handled by a JavaScript function. This function has full access to the data that is entered by the user into a form.

How are forms created in React?

class Display extends React.Component({ show(evt) { // code }, render() { // Render the div with an onClick prop (value is a function) return ( <div onClick={this.show}>Click Me!</div> ); } });

How do you create an event in React?

Flux: The Store contains state and change logic Redux: Store and change logic are separate Flux: There are multiple stores Redux: There is only one store Flux: All the stores are disconnected and flat Redux: Single store with hierarchical reducers Flux: Has singleton dispatcher Redux: No concept of dispatcher Flux: React components subscribe to the store Redux: Container components utilize connect FluxState is mutable 6. State is immutable

How is Redux different from Flux?

Just like how React is based on components, in React Router v4, the API is 'All About Components'. A Router can be visualized as a single root component (<BrowserRouter>) in which we enclose the specific child routes (<route>). No need to manually set History value: In React Router v4, all we need to do is wrap our routes within the <BrowserRouter> component. The packages are split: Three packages one each for Web, Native and Core. This supports the compact size of our application. It is easy to switch over based on a similar coding style.

List down the advantages of React Router.

It increases the application's performance It can be conveniently used on the client as well as server side Because of JSX, code's readability increases React is easy to integrate with other frameworks like Meteor, Angular, etc Using React, writing UI test cases become extremely easy

List some of the major advantages of React

Higher Order Component is an advanced way of reusing the component logic. Basically, it's a pattern that is derived from React's compositional nature. HOC are custom components which wrap another component within it. They can accept any dynamically provided child component but they won't modify or copy any behavior from their input components. You can say that HOC are 'pure' components. **need to look into this more

What are Higher Order Components(HOC)?

Predictability of outcome - Since there is always one source of truth, i.e. the store, 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 render 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.

What are the advantages of Redux?

Initial Rendering Phase: This is the phase when the component is about to start its life journey and make its way to the DOM Updating Phase: Once the component gets added to the DOM, it can potentially update and re-render only when a prop or state change occurs. That happens only in this phase. Unmounting Phase: This is the final phase of a component's life cycle in which the component is destroyed and removed from the DOM.

What are the different phases of React component's lifecycle?

React is just a library, not a full-blown framework. As such when building something with React you will have to include other libraries to handle other parts of an application, such as application state. React is mainly written using JSX & ES6, transpiled using Babel and build & packaged using Webpack & npm. Those tools are requirements to be effective when using React, however their inner workings & processes are not easy to learn and require significant time to understand. On the other hand if you skip learning those things, your build might fail with error messages that have no meaning to you and you will be stuck with code that doesn't work and you have no power to correct by yourself.

What are the limitations of React?

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. 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. 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 depends solely on the values of their arguments.

What are the three principles that Redux follows?

Controlled: do not maintain their own state, data is controlled by the parent component, they take the current values through props and then notify the changes via callbacks. Uncontrolled Components: They maintain their own state, Data is controlled by the Dom, Refs are used to get their current values

What do you know about controlled and uncontrolled components?

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.

What do you understand by "Single source of truth"?

In React, for every DOM object, there is a corresponding "virtual DOM object." A virtual DOM object is a representation of a DOM object, like a lightweight copy. It can't change HTML directly. The entire virtual DOM gets updated. The virtual DOM gets compared to what it looked like before you updated it. React figures out which objects have changed. The changed objects, and the changed objects only, get updated on the real DOM. Changes on the real DOM cause the screen to change.

What do you understand by Virtual DOM? Explain its working

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.

What do you understand by refs in React?

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.

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

JSX is a shorthand for JavaScript XML. This is a type of file used by React which utilizes the expressiveness of JavaScript along with HTML like template syntax. This makes the HTML file really easy to understand. This file makes applications robust and boosts its performance. TLDR: lets you write HTML in a JS file

What is JSX?

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.

What is React Router?

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.

What is Redux?

States are the objects which determine components rendering and behavior. They are mutable unlike the props and create dynamic and interactive components. They are accessed via this.state().

What is a state in React and how is it used?

In React, events are the triggered reactions to specific actions like mouse hover, mouse click, key press, etc. Handling these events are similar to handling events in DOM elements. But there are some syntactical differences like: Events are named using camel case instead of just using the lowercase. Events are passed as functions instead of strings. The event argument contains a set of properties, which are specific to an event. Each event type contains its own properties and behavior which can be accessed via its event handler only.

What is an event in React?

Arrow functions are more of brief syntax for writing the function expression. They are also called 'fat arrow' (=>) the functions. These functions allow to bind the context of the components properly since in ES6 auto binding is not available by default. Arrow functions are mostly useful while working with the higher order functions.

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

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 the 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.

What is the significance of Store in Redux?

DOM manipulation was very expensive Applications were slow and inefficient There was huge memory wastage Because of circular dependencies, a complicated model was created around models and views

What were the major problems with MVC framework?


संबंधित स्टडी सेट्स

Chapter 4 Installing and Configuring the Dynamic Host Configuration Protocol

View Set

What is a partnership and how does it work

View Set

Quiz 20 Fundamentals of organizational structure

View Set

Chapter 14, Section 14.2: Computing Partial Derivatives Algebraically

View Set