React Js Interview Questions And Answers
1. Why Reactjs Is used?
Answer: Reactjs is used to handle only the View part and not for other web apps as well as mobile apps. (react js interview questions and answers)
2. What Are the features of React?
Answer:
- JSX – The JSX is JavaScript Syntax Extension
- Components – The Reactjs is all about components
- One Direction Flow (Unidirectional Flux) – The React.js implements one-way data flow, which makes it easy to reason about your apps
The List of Major Features as following:
- React uses the virtual DOM instead of the real DOM.
- React uses server-side rendering.
- React follows unidirectional data-binding.
3. What Are the Advantages of Reactjs?
Answer:
The List of Reactjs Advantages as following as:
- Reactjs uses virtual DOM which will improve the performance of the apps.
- Reactjs is free and Open Source and very fast.
- Reactjs improves SEO Performance.
- It can be used for both client and server-side.
- Also, it can be used with other frameworks such as Angular, Meteor, etc.
- It is very easy to write UI test cases because the virtual DOM system implemented entirely in JavaScript.
- It increases the application’s performance
- Reactjs code readability increases by using the JSX.
4. What Is Uncontrollable Component in Reactjs??
Answer: The uncontrollable component is a component which stores its own state internally.
5. What Is a state in Reactjs and how is it used?
Answer:
- The states are the heart and soul of Reactjs components. The states are objects, source of data which determine components rendering and its behavior.
- The states are mutable and it used to create dynamic, stateful and interactive web and mobile components by using the – this.state().
6. What Is Controllable Component in Reactjs?
Answer:
- A controlled component is a component where React is in control and takes the value through Props and changes are notified to the parent component through callbacks.
- Parent component controls this component either bypassing the data and handling the callbacks and managing its own state.
7. Are You Aware of the Difference Between Props and State?
Answer:
- As you know that there is a very thin line between the two, it is important to ask this question during the interview. Where the states are mutable, slower and used as places where the data comes from, the props are exactly different. These are used to pass the data from the top-level components.
- Define Higher-Order Components It is not possible for a candidate to implement a basic HOC pattern, as it will take more time than typically allotted for an interview. Instead, you can ask him or her to definition HOC. Higher-order components are the React components that include other components.
- Asking these questions to the candidate will cover all the aspects of the technology. Isn’t it a great idea to have a list of questions related to the basic aspects of React handy? Thus, these can be typically asked while hiring a remote React developer.
8. Is it possible to nest JSX elements into other JSX elements?
Answer: It is possible. The process is quite similar to that of nesting the HTML elements. However, there are certain things that are different in this. You must be familiar with the source and destination elements to perform this task simply.
9. Re-Render on Changes?
Answer: In addition to props, components can also have an internal state. The most prominent example of that behavior would be a click counter that updates its value when a button is pressed. The number of clicks itself would be saved in the state.
10. Explain DOM diffing?
Answer: When the components are rendered twice, Virtual Dom begins checking the modifications elements have got. They represent the changed element on the page simply. There are several other elements that don’t go through changes. To cut down the changes to the DOM as an outcome of user activities, DOM doffing is considered. It is generally done to boost the performance of the browser. This is the reason for its ability to perform all the tasks quickly.
11. What is FLUX in ReactJs?
Answer: Flux is an application architecture in React view Library which designed by Facebook for creating data layer in an application based on JavaScript.
12. Explain lifecycle methods of React components?
Answer:
- Component Will Mount: Executed before rendering and is used for App-level configuration in your root component.
- Component Did Mount: Executed after first rendering and here all AJAX requests, DOM or state updates, and set up event listeners should occur
- Component Will Receive Props: Executed when particular prop updates to trigger state transitions.
- Should Component Update: Determines if the component will be updated or not. By default, it returns true. If you are sure that the component doesn’t need to render after state or props are updated, you can return a false value. It is a great place to improve performance as it allows you to prevent a re-render if component receives new prop.
- Component Will Update: Executed before re-rendering the component when there are props & state changes confirmed by should Component Update() which returns true.
- Component Did Update: Mostly it is used to update the DOM in response to prop or state changes.
- Component Will Unmount: It will be used to cancel any outgoing network requests, or remove all event listeners associated with the component.
13. What are your archnemesis front-end issues?
Answer:
- If you are like me, you have issues that come up in every project. Mines are animation orchestration and complex routing patterns. I could speak hours about why I can’t get them right and what I hate about the libraries that I use.
- Letting you choose the topic keeps the conversation comfortable and it is a good check on the limits of your knowledge.
- Be honest if a question like this comes up. You can show a lot of expertise by reasoning about complex issues that you just can’t get right. We might even find a common enemy to hate.
14. How would your performance optimize a React app?
Answer:
- Developers who never met with performance issues would answer this differently than battle-hardened veterans. It should clearly indicate if you’re stuck with quoting articles, or you actually measured, iterated and fixed real performance issues in the past.
- Talking about micro-optimizations and broad ideas show the former while mentioning issues, measurements and development tools – that you used in the past – signals the latter.
- If you are experienced with performance optimization, do not start speaking about the general concepts. Get straight to the specific tools and methods you used.
15. What are closures and lexical scope?
Answer: A closure is when a function is able to remember and access its lexical scope even when that function is executed outside of that scope. A good example of closures is currying, a commonly accepted answer is also responding that an inner function returned by an outer function has access to the variables of its outer function even after the outer function was called. Lexical scope is the scope model used by JavaScript which compared to dynamic scoping enables a lot of cool features. If someone also explains lexing time and goes into hoisting it is a big bonus. (online training institute)
16. What are the lifecycle methods?
Answer: Each reacts component has several methods which enable the developer to hook into a component’s lifecycle. React provides methods for hooking into the mounting, un-mounting, updating or error handling states off a component. Such methods are for instance component Did Mount, render, component Did Catch, etc.
17. How do you handle method binding in components?
Answer: There are more ways, we can bind the method in the render, in component Did Mount or use the preferred way, which is arrow functions.
18. Explain concepts of functional programming?
Answer: Basic concepts are pure functions, side effects, data mutation and declarative over imperative. On top of that currying, compose and function composition are important concepts. Knowledge of libraries like RamdaJS provides useful utilities to build applications in a more functional way. Functions being a first-class citizen make it possible for JS to be really functional is important as well.
19. Enlist React limitations if you know any?
Answer: Though React is a popular tool but has some limitations that are listed below:
- React is not a framework but a set of libraries.
- Designers take time to understand this library as it is very large in size
- For novice programmers, it can be difficult to understand this library
- Inline templating and JSX makes the coding somewhat complex
20. How Does react Virtual DOM Work?
Answer: In react, Virtual DOM is a node tree that has lists of elements and their attributes, content as object and their properties. React js methods create node tree react components. It updates the node tree in response to a mutation in the data model.
Virtual DOM representation the user interface has created. If you are thinking about how many phases to update the browser’s DOM.
21. What is ‘setState’ used for?
Answer: When you issue a ‘setState’ command, an object is merged into the current state. After this is done, the UI is updated according to the new set state.
22. What is the difference between a Container and a Component?
Answer:
- The component is part of the React API. A Component is a class or function that describes part of a React UI.
- The 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.
23. What are the features of ReactJS?
Answer:
- One way data flow
- Virtual Document Object Model
- javaScript Extension(JSX)
- React native
- Architecture beyond HTML
24. Handling Multiple Platforms?
Answer:
- React Native gracefully handles multiple platforms. The vast majority of the React Native APIs are cross-platform, so you just need to write one React Native component, and it will work seamlessly on both iOS and Android. Facebook claims that its Ad Manager application has 87% code reuse across the two platforms, and I wrote a flashcard app without any platform-specific code at all.
- If you do want to write platform-specific code — due to different interaction guidelines on iOS and Android, for instance, or because you want to take advantage of a platform-specific API — that’s easy, too. React Native allows you to specify platform-specific versions of each component, which you can then integrate into the rest of your React Native application.
25. Passing functions between components in React and React Native?
Answer:
- I would stay away from such passing functions between components. I always use Flux architecture with ReactJs and React Native.
- Keep components just to render stuff by properties and sending new actions.
- You have a hard dependency between components. This stuff does not scale. It will be hard to maintain such code.
- What I personally do is just write actions, stores, dispatcher and don’t use any dependency on stuff like redux, because React Native is evolving rapidly and you never know if your dependencies will do it at the same speed.
26. What are the different phases of React component’s lifecycle?
Answer:
There are four different phases of React component’s lifecycle:
- 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.
27. What is a state in React and how is it used?
Answer: States are the heart of React components. States are the source of data and must be kept as simple as possible. Basically, 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()
28. What is Lifting State Up in React?
Answer: When several components need to share the same changing data then it is recommended to lift the shared state up to their closest common ancestor. That means if two child components share the same data from its parent, then move the state to parent instead of maintaining local state in both of the child components.
29. What is the purpose of using the super constructor with props argument?
Answer: A child class constructor cannot make use of this reference until the super() method has been called. The same applies to ES6 sub-classes as well. The main reason for passing props parameter to super() call is to access this.props in your child constructors.
30. How React works? How Virtual-DOM works in React?
Answer:
- 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.
- The HTML DOM is always tree-structured — which is allowed by the structure of the HTML document. The DOM trees are huge nowadays because of large apps. Since we are more and more pushed towards dynamic web apps (Single Page Applications — SPAs), we need to modify the DOM tree incessantly and a lot. And this is a real performance and development pain.
- The Virtual DOM is an abstraction of the HTML DOM. It is lightweight and detached from the browser-specific implementation details. It is not invented by React but it uses it and provides it for free. ReactElements lives in the virtual DOM. They make the basic nodes here. Once we defined the elements, ReactElements can be rendered into the “real” DOM.
- Whenever a ReactComponent is changing the state, diff algorithm in React runs and identifies what has changed. And then it updates the DOM with the results of diff. The point is – it’s done faster than it would be in the regular DOM.
31. What’ss wrong with following code?
Answer: Nothing isn’t right with it. It’s once in a while utilized and not outstanding, but rather you can likewise pass a function to setState that gets the past state and props and returns another state, similarly as we’re doing above. Furthermore, is nothing amiss with it, as well as effectively recommended in case you’re setting state in light of the previous state.
32. What are pure functional Components?
Answer:
- Traditional React Components as we have seen so far is making a class with class Example extends React. Component or React. create Class(). These make stateful components on the off chance that we at any point set the state (i.e. this. set State(), get Initial State(), or this.state = {} inside a constructor()).
- In the event that we have no expectation for a Component to require state, or to require lifecycle methods, we can really compose Components with a pure function, consequently the expression “pure function Component”:
33. Is it possible to use the word “Class” in JSX? Why or why not?
Answer: No, it is not possible in the JSX. This is because the word “Class” is a reticent (occupied) word in the JavaScript. However, you can use you are free to use the word “ClassName”. If you use the word “Class” the JSX will be translated to JavaScript immediately.
34. What is ‘Store’ in Redux?
Answer: Store is the object that holds the application state and provides a few helper methods to access the state, dispatch actions and register listeners. The entire state is represented by a single store. Any action returns a new state via reducers. That makes Redux very simple and predictable.import { create store } from ‘redux’;
35. How do you cope with teams?
Answer:
- Working with a team is a struggle and it is worth a question. We could discuss how you track your issues, what git-flow do you prefer and if you like pair coding or occasional meetings more.
- Team working is an important ability and it really shows if someone has no experience with it. If you never worked with a team before, be frank and tell it though.
- Having a newcomer can be better than a burnt-out veteran with trust issues from all the experiences.
36. What is hoisting?
Answer: A common answer is: hoisting is a mechanism which moves all declarations to the top of the execution scope. A good answer is understanding that JavaScript compiles your code before execution. When the compiler enters an execution context, it will start lexing (splitting your code) and analyzing the code while creating a list out of the declarations it finds. Each time the compiler encounters a declaration, it will create a binding to it and each time it encounters an assignment or evaluation it will ask the current scope for the binding, if it can’t find the binding, it will go up until it reaches the global scope. If you have a strict mode on, you will get an error and if you use the good old es5 it will create a new binding for you. This is why you are able to assign to a variable which wasn’t declared before. Anyway, after running through some steps, it will generate some compiled code which can be then executed.
37. What is the difference between const, let and var?
Answer: var is global or function scoped while let and const are blocks scoped. Let and const can’t be redeclared although let can be reassigned. Saying that const should always be preferred where possible is a big plus as JavaScript moves towards functional programming and we don’t want to change/re-assign existing variables.
38. What are the actions and action creators?
Answer: An action is an object with a mandatory type field, it usually has also a payload which are the data related to the action. An action creator is a function which returns an action.
39. Do you know Lerna or what a monorepo is?
Answer: Lerna is a tool used to handle mono repositories and package dependencies. Essentially, instead of having a large monolithic application, we break it down into small independent packages kept in one repository.
40. Compare props and state?
Answer:
Following are a few noticeable points for a quick comparison of prop and state:
- Both Props and State receive initial value from the parent component
- In-State, the parent component can change the value, while in case of props it is not so.
- Default values can be set in both state and prop
- Components can be changed from inside in case of State while this cannot be done in Props
- The initial value can be set for child component in both of the cases
41. What is Props?
Answer: Props are Properties of React. It is immutable Properties of React, In another word you can’t change the value of props. 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. we can also say, Props are communication channel of two different component. It is using to render the dynamically generated data and helping to maintain the unidirectional data flow.
42. What is Higher-Order Components in React?
Answer: A higher-order component is an advanced way to re-using component logic and it is a function that takes a component and returns a new component that is known as Higher-Order Components.
43. Why virtual dom is faster?
Answer:
Virtual dom is faster because it uses:
- Efficient diff algorithm
- Batched update operations
- Efficient update of subtree only
- Uses observable instead of dirty checking to detect a change
Note: ReactJS maintains two virtual DOM, one of the updated state Virtual DOM and another with the prior state Virtual DOM.
44. Why should we use ReactJS?
Answer: As you can probably tell, this is one of the more opinion-based ReactJS interview questions. You would do well, however, if you simply state some of the best features that are accredited to React.
These include, but are not limited to:
Virtual DOM instead of a real DOM;
Fast and scalable;
JSX provides a code that’s easy to both read and write;
React is easily integrated with other JavaScript frameworks;
etc.
45. What Are the differences in between Real DOM and Virtual DOM?
Answer:
Real DOM:
- It updates slow as compare to Virtual DOM
- We can directly update the HTML on DOM at the run time.
- Will creates a new DOM if element updates
- DOM manipulation is a bit expensive
- There is too much memory wastage
Virtual DOM:
- It updates faster as compare to Real DOM
- We can’t directly update the HTML on DOM at the run time.
- Will need to updates the JSX if element updates
- DOM manipulation is easy as compare to the real DOM.
- There is no memory wastage
46. What Are the lifecycle methods of Reactjs components?
Answer:
The life cycle methods of the Reactjs are:
Component Will Mount: This method executed just before rendering takes place both on the client and server.
Component Did Mount: This method executed on the client-side only after the first render.
Component Will Receive Props: This method invoked as soon as the props are received from the parent class and before another render is called.
Should Component Update: This method returns true or false value based on certain conditions.
Component Will Update: This method called just before rendering takes place in the DOM.
Component Did Update: This method called immediately after rendering takes place.
Component Will Unmount: This method called after the component is unmounted from the DOM and used to clear the memory spaces.
47. Mention the key benefits of Flux?
Answer: Applications that are built on Flux have components which can simply be tested. By simply updating the store, developers are able to manage and test any react component. It cut down the overall risk of data affection. All the applications are highly scalable and suffer no compatibility issues.
48. What are the differences between a class component and functional component?
Answer:
- Class components allow us to use additional features such as local state and lifecycle hooks. Also, to enable our component to have direct access to our store and thus holds state.
- When our component just receives props and renders them to the page, this is a ‘stateless component’, for which a pure function can be used. These are also called dumb components or presentational components.
- From the previous question, we can say that our Booklist component is functional components and are stateless.
49. What do you understand by Virtual DOM? Explain its working?
Answer: A virtual DOM is a lightweight JavaScript object which originally is just the copy of the real DOM. It is a node tree that lists the elements, their attributes, and content as Objects and their properties. React’s render function creates a node tree out of the React components. It then updates this tree in response to the mutations in the data model which is caused by various actions done by the user or by the system.
50. Can you use ref inside a functional component?
Answer: No, you cannot use ref inside a functional component since functional components are stateless and they produce the same output. However, ref attribute is used to get the reference to a DOM node or an instance of a component in React and then manipulate its instance.
51. How are forms created in React?
Answer: 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.
52. Explain the role of Reducer?
Answer: 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.
53. What is render() to React? And explain its purpose?
Answer: 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 This function must be kept pure i.e., it must return the same result each time it is invoked.
Note: Browse latest React JS Interview Questions and Angular training videos. Here you can check Angular JS Online Training details and React Js Training Videos for self learning. Contact +91 988 502 2027 for more information.