DEV Community

Isha Saxena
Isha Saxena

Posted on

React Interview Questions Exp 2-3 yrs

Remove duplicate elements from an array
arr=[1,2,1,1,3,4,5,5]
arr.reduce(function(accumulator,currentValue){
if(accumulator.indexOf(currentValue)===-1){
accumulator.push(currentValue)
}
return accumulator
},[])

What are the main features/advantages of using React?

  1. Uses Virtual DOM whenever a React application gets changes in state/prop it will re-render itself during re-render React uses Virtual DOM, Virtual DOM is an in memory representation of Real DOM whenever the application re-renders React will create a new Virtual DOM and will compare it with already existing Virtual DOM uses diffing algorithm and will update only those nodes which have changed only .

  2. React uses JSX which is JavaScript XML it is a syntactic sugar which makes us be able to develop a component and write its HTML and JavaScript together

3.React has unidirectional data flow where data can only flow from Parent to child or top to bottom .

4.React follows a component based architechture where single reusable pieces of codes are classified into components and are further assembled together to develop User interface

What are problems like prop drilling in react?

So basically as we know in React the data flows from top to bottom like from parent to child then suppose we have a 5 level of component based application data flows from A->B->C->D->E
now you want to transfer data from A to E this will make your code less maintainable and suppose B,C,D do not want to use your data then its code duplication also so to avoid this we use global state management features in React like
Context API and Redux

4.What are useMemo() and useCallBack() hooks in react used for?

So sometimes when we are doing some expensive calculation during re-rendering of our application this expensive calculation will happen again and again on any state/prop change even if suppose this is sum of thousand numbers so even if only THEME of application is changed and it has no relation to sum of 1000 nos still this calculation will be done again

THAT IS WHERE THE PROBLEM IS!!!!
Doing expensive calculation again and again when not needed !!
So making the performance of application SLOWWWWWWWW!!!
SOLUTION

useMemo() hook will cache or memoize the result of these expensive calculation it accepts a value to be remembered and a dependency
useMemo(Value to be memoized,dependecy)
useMemo(sumofThousandNumbersResult,inputNum)

Sameway works useCallBack() except that it will cache whole function and will only re-run this functn when actually needed

useCallback(callback function,dep)

Top comments (1)

Collapse
 
rishabh_123 profile image
Rishabh s

Great