React Developer
100+ React Developer Interview Questions and Answers
Q1. Swap Two Numbers Problem Statement
Given two integers a
and b
, your task is to swap these numbers and output the swapped values.
Input:
The first line contains a single integer 't', representing the number of t...read more
Swap two numbers 'a' and 'b' and output the swapped values.
Create a temporary variable to store one of the numbers
Assign the value of 'a' to 'b' and the temporary variable to 'a'
Output the swapped values as 'b' followed by 'a'
Q2. Covid Vaccination Distribution Problem
As the Government ramps up vaccination drives to combat the second wave of Covid-19, you are tasked with helping plan an effective vaccination schedule. Your goal is to ma...read more
Maximize the number of vaccines administered on a specific day while adhering to certain rules.
Given n days, maxVaccines available, and a specific dayNumber, distribute vaccines to maximize on dayNumber
Administer positive number of vaccines each day with a difference of 1 between consecutive days
Ensure sum of vaccines distributed does not exceed maxVaccines
Output the maximum number of vaccines administered on dayNumber for each test case
React Developer Interview Questions and Answers for Freshers
Q3. Triplets with Given Sum Problem
Given an array or list ARR
consisting of N
integers, your task is to identify all distinct triplets within the array that sum up to a specified number K
.
Explanation:
A triplet i...read more
The task is to find all distinct triplets in an array that sum up to a specified number.
Iterate through the array and use nested loops to find all possible triplets.
Keep track of the triplets that sum up to the target number.
Handle cases where no triplet exists for a given target sum.
Q4. Sort Array Problem Statement
Given an array consisting of 'N' positive integers where each integer is either 0, 1, or 2, your task is to sort the given array in non-decreasing order.
Input:
Each input starts wi...read more
Sort an array of positive integers consisting of 0, 1, and 2 in non-decreasing order.
Use a counting sort approach to count the occurrences of 0s, 1s, and 2s in the array.
Update the array with the counts of 0s, 1s, and 2s in order to achieve the sorted array.
Ensure to handle the constraints of the input array elements being 0, 1, or 2.
Example: Input: [0, 2, 1, 2, 0], Output: [0, 0, 1, 2, 2]
Q5. How do you connect a component to Redux store? Which function in Redux is used to connect to store? What are the parameters in connect?
To connect a component to Redux store, we use the connect function from the react-redux library.
Import the connect function from react-redux.
Use the connect function to wrap the component and connect it to the Redux store.
The connect function takes two parameters: mapStateToProps and mapDispatchToProps.
mapStateToProps is a function that maps the state from the Redux store to the component's props.
mapDispatchToProps is an optional function that maps the dispatch function to th...read more
Q6. Furthest Building You Can Reach
In this problem, Ninja prefers jumping over building roofs rather than walking through the streets. The heights of the buildings in his city are represented by an array HEIGHTS
, ...read more
Given heights of buildings, bricks, and ladders, find the farthest building Ninja can reach using them optimally.
Iterate through the buildings and calculate the difference in heights between consecutive buildings.
Use bricks or ladders to cover the height difference when needed.
Keep track of the farthest building Ninja can reach based on available bricks and ladders.
Return the index of the farthest building Ninja can reach.
Share interview questions and help millions of jobseekers 🌟
Q7. What are Hooks in React? Name the ones you have used in your project.
Hooks are a feature introduced in React 16.8 that allow developers to use state and other React features in functional components.
useState() - for managing state in functional components
useEffect() - for performing side effects in functional components
useContext() - for accessing context in functional components
useReducer() - for managing complex state and actions in functional components
useCallback() - for memoizing functions in functional components
useMemo() - for memoizing...read more
Q8. Write a React Class component. Convert this Class to a Functional Component. How can you pass prop from parent to child component? Write code.
Answer to a React Developer interview question about class and functional components and passing props.
Class component: class MyComponent extends React.Component {}
Functional component: const MyComponent = (props) => {}
Passing props from parent to child:
React Developer Jobs
Q9. How do you avoid re-rendering of a component? With useEffect second parameter, should ComponentUpdate
To avoid re-rendering, use shouldComponentUpdate or React.memo
Use shouldComponentUpdate to compare current and next props/state
Use React.memo to memoize functional components
Use useMemo to memoize expensive computations
Use useCallback to memoize event handlers
Use PureComponent for class components
Promises in JavaScript represent the eventual completion (or failure) of an asynchronous operation.
Promises are objects that represent the eventual completion (or failure) of an asynchronous operation.
They have three states: pending, fulfilled, or rejected.
A pending promise is one that hasn't been fulfilled or rejected yet.
A fulfilled promise is one that has been successful.
A rejected promise is one that has encountered an error.
Promises help in handling asynchronous operatio...read more
Functional components are simpler and easier to test, while class components have more features and lifecycle methods.
Functional components are written as JavaScript functions, while class components are written as ES6 classes.
Functional components are stateless and do not have their own internal state, while class components can have state.
Functional components do not have lifecycle methods, while class components have lifecycle methods like componentDidMount and componentDi...read more
Q12. What are Higher Order Functions and Higher Order Components. Give examples.
Higher Order Functions are functions that take other functions as arguments or return functions as their results.
Higher Order Functions can be used to create reusable code by abstracting common functionality into a separate function.
They can also be used to implement functional programming concepts like currying and composition.
Example: Array.prototype.map() is a higher order function that takes a callback function as an argument and applies it to each element of an array, re...read more
Q13. How does Event Loop works? What are Event Queue and Event Stack?
Event Loop is a mechanism that allows JavaScript to handle asynchronous operations.
Event Loop is a continuous process that checks the Event Queue and moves events to the Event Stack.
Event Queue holds all the events that are waiting to be processed.
Event Stack holds the events that are currently being processed.
When the Event Stack is empty, the Event Loop checks the Event Queue for new events.
JavaScript uses Event Loop to handle asynchronous operations like setTimeout(), setI...read more
Q14. What are refs? How will you use it for getting input value? Explain with code.
Refs are a way to access DOM nodes or React components directly. They can be used to get input values.
Refs provide a way to access DOM nodes or React components directly.
They are commonly used to get input values or trigger imperative animations.
Refs can be created using the `createRef()` method or by using a callback function.
To get the value of an input using refs, you can assign a ref to the input element and access its value property.
Q15. What is Context API in React? Is there a need to have an initial state in Context API?
Context API is a way to share data between components without passing props down manually.
Context API provides a way to pass data through the component tree without having to pass props down manually at every level.
It is useful for sharing data that needs to be accessed by many components at different levels of the tree.
Initial state can be set in Context API using the createContext() function.
There is no need to have an initial state in Context API, but it can be useful in s...read more
Q16. What are the ways to handle Errors in React?
Error handling in React can be done using try-catch blocks, error boundaries, and handling asynchronous errors.
Use try-catch blocks to handle synchronous errors
Use error boundaries to catch errors in child components
Handle asynchronous errors using promises or async/await
Use third-party libraries like Sentry or Bugsnag for better error tracking
Display user-friendly error messages to improve user experience
TRUNCATE is a DDL command that removes all records from a table, while DELETE is a DML command that removes specific records.
TRUNCATE is faster than DELETE as it does not log individual row deletions.
TRUNCATE resets the auto-increment value of the table, while DELETE does not.
TRUNCATE cannot be rolled back, but DELETE can be rolled back using transactions.
TRUNCATE does not trigger any triggers associated with the table, while DELETE does.
Q18. What is the minimum coverage for an app? How much code do you push to production/master branch while deployment?
Minimum coverage for an app and code pushed to production/master branch while deployment.
Minimum coverage for an app depends on the project requirements and complexity.
Generally, a coverage of 80% or higher is considered good.
Code pushed to production/master branch should be thoroughly tested and reviewed.
Continuous integration and deployment can help automate this process.
Examples of tools for code coverage include Jest, Istanbul, and Enzyme.
Top-level directories in Redux should be structured based on functionality and feature modules.
Separate directories for actions, reducers, and components
Group related functionality together in separate directories
Use feature modules to encapsulate related actions, reducers, and components
Example: 'actions', 'reducers', 'components', 'utils', 'constants'
Q20. What are export types in ReactJS?
Export types in ReactJS allow components, functions, and variables to be accessed and used in other files.
Exporting a component allows it to be imported and used in other files
Exporting a function allows it to be imported and used in other files
Exporting a variable allows it to be imported and used in other files
Q21. How do you hide API URLs? How to manage different URLs for different staging environment?
API URLs can be hidden by using environment variables and managing different URLs for different staging environments.
Use environment variables to store API URLs
Create separate environment files for different staging environments
Set the appropriate API URL in the environment file for each staging environment
Access the API URL from the environment variable in the code
Q22. Do Reducer need to have an initial state compulsorily?
Yes, it is mandatory for Reducer to have an initial state.
Initial state defines the starting point of the state tree.
It helps in defining the shape of the state tree.
It is used to set default values for the state properties.
It is also used to reset the state to its initial values.
Example: const initialState = { count: 0 };
Example: const initialState = { name: '', age: 0 };
Class components are ES6 classes that extend from React.Component and have access to state and lifecycle methods, while functional components are simple functions that take props as arguments and return JSX.
Class components are defined using ES6 classes and extend from React.Component
Functional components are simple functions that take props as arguments and return JSX
Class components have access to state and lifecycle methods, while functional components do not
Functional com...read more
Stateless components do not have internal state, while stateful components have internal state.
Stateless components are functional components that do not have internal state.
Stateful components are class components that have internal state.
Stateless components are simpler and easier to test.
Stateful components are more complex and can hold and update internal state.
Example: Stateless component - const Button = () => <button>Click me</button>
Example: Stateful component - class...read more
mapStateToProps() is used to access the Redux state in a component, while mapDispatchToProps() is used to dispatch actions to update the state.
mapStateToProps() is used to access the Redux state and return data as props for a component.
mapDispatchToProps() is used to dispatch actions to update the Redux state.
mapStateToProps() is a function that takes the current state as an argument and returns an object with props that will be passed to the component.
mapDispatchToProps() is...read more
The states of a promise object are pending, fulfilled, or rejected.
A promise starts in the pending state.
When a promise is resolved, it transitions to the fulfilled state.
If a promise encounters an error, it transitions to the rejected state.
Once a promise is settled (fulfilled or rejected), it cannot transition to any other state.
Q27. What is React? Why do we use it? Make a form with 3 different child components and pass props from the parent. There should be Select box, an Input field and a Submit Button.
React is a JavaScript library used for building user interfaces.
React allows for reusable components and efficient rendering.
It uses a virtual DOM to update only the necessary parts of the UI.
React is popular for its simplicity and flexibility.
Examples of companies using React include Facebook, Instagram, and Airbnb.
Q28. Explain Redux-Saga middleware. How do you Dispatch actions from components in Redux?
Redux-Saga is a middleware for Redux that handles side effects. Actions can be dispatched from components using mapDispatchToProps.
Redux-Saga is used to handle side effects like API calls and asynchronous actions.
It uses generator functions to make asynchronous code look synchronous.
Sagas listen for specific actions and perform tasks based on those actions.
To dispatch actions from components, use mapDispatchToProps to connect the component to the Redux store.
Then call the act...read more
Q29. Will React re-render whole page or just a part of it?
React re-renders just the part of the page that has changed, thanks to its virtual DOM.
React uses a virtual DOM to track changes in the UI.
When a component's state or props change, React compares the virtual DOM with the real DOM and updates only the necessary parts.
This approach improves performance by minimizing the number of DOM manipulations.
React's diffing algorithm efficiently determines the minimal set of changes needed to update the UI.
slice() returns a shallow copy of a portion of an array without modifying the original array, while splice() changes the contents of an array by removing or replacing existing elements.
slice() does not modify the original array, while splice() does
slice() returns a new array, while splice() returns the removed elements
slice() takes start and end index as arguments, while splice() takes start index, number of elements to remove, and optional elements to add as arguments
Example...read more
No, const does not make the value immutable.
const only makes the variable itself immutable, not the value it holds.
For objects and arrays, const prevents reassignment but allows mutation of properties or elements.
To make a value truly immutable, you can use Object.freeze() or other techniques.
Use the built-in sort() method to sort an array of integers in JavaScript.
Use the sort() method with a compare function to sort the array in ascending order.
For descending order, modify the compare function to return b - a instead of a - b.
Example: const numbers = [4, 2, 5, 1, 3]; numbers.sort((a, b) => a - b);
Q33. How Promise works? What is Promise.all. Write code for both.
Promises are a way to handle asynchronous operations in JavaScript. Promise.all is used to execute multiple promises concurrently.
Promises represent a value that may not be available yet
They have three states: pending, fulfilled, and rejected
Promise.all takes an array of promises and returns a new promise that resolves when all promises in the array have resolved
If any promise in the array is rejected, the returned promise is rejected with the reason of the first promise that...read more
Q34. What are Hooks in React? Explain useState, useEffect hooks.
Hooks are functions that allow you to use state and other React features without writing a class.
useState is a hook that allows you to add state to functional components.
useEffect is a hook that allows you to perform side effects in functional components.
Hooks can only be used in functional components.
Hooks must be called at the top level of a functional component.
Hooks can be used to replace lifecycle methods in class components.
Functional Components are simple functions that take props as input and return JSX, while Class Components are ES6 classes that extend React.Component and have additional features like state and lifecycle methods.
Functional Components are simpler and easier to read/write compared to Class Components.
Functional Components do not have access to state or lifecycle methods, while Class Components do.
Functional Components are stateless, while Class Components can be stateful.
Funct...read more
Q36. Difference between Let, Const and Var. Write code and explain.
Let, Const, and Var are used to declare variables in JavaScript with different scoping and reassignment abilities.
Var has function scope and can be redeclared and reassigned.
Let has block scope and can be reassigned but not redeclared.
Const has block scope and cannot be reassigned or redeclared.
Q37. How can you optimize a React App?
Optimizing a React app involves reducing bundle size, using lazy loading, and optimizing rendering performance.
Reduce bundle size by code splitting and using dynamic imports
Use lazy loading to load components only when needed
Optimize rendering performance by using shouldComponentUpdate and PureComponent
Use React.memo to memoize functional components
Avoid unnecessary re-renders by using useMemo and useCallback
Use performance profiling tools like React DevTools and Chrome DevTo...read more
Q38. What are Hooks? What all Hooks have you used in your project?
Hooks are functions that allow you to use state and other React features without writing a class.
useState() - for managing state in functional components
useEffect() - for performing side effects in functional components
useContext() - for consuming context in functional components
useReducer() - for managing complex state in functional components
useCallback() - for memoizing functions in functional components
useMemo() - for memoizing values in functional components
useRef() - fo...read more
Q39. What is Lazy Loading, Suspense. How do they work?
Lazy Loading and Suspense are techniques used to improve performance by loading components and data only when needed.
Lazy Loading delays the loading of non-critical resources until they are needed, reducing initial load time.
Suspense allows components to wait for data to load before rendering, improving user experience.
Lazy Loading and Suspense can be used together to optimize performance and user experience.
Example: A website with a large image gallery can use Lazy Loading t...read more
Q40. Features of ES6. Explain Spread Operator and Rest Parameter by writing code. Give example for Object Destructuring.
ES6 features: Spread Operator, Rest Parameter, Object Destructuring
Spread Operator: allows an iterable to be expanded into individual elements
Rest Parameter: allows a function to accept an indefinite number of arguments as an array
Object Destructuring: allows extracting properties from an object and assigning them to variables
Q41. How to implement Responsiveness in App as per screen sizes?
Responsiveness in app can be achieved using CSS media queries and responsive design principles.
Use CSS media queries to apply different styles based on screen sizes
Design the app layout to be flexible and adapt to different screen sizes
Use responsive units like percentages and viewport units for sizing elements
Test the app on different devices and screen sizes to ensure responsiveness
Q42. What is the significance of 'this' keyword in JS?
The 'this' keyword in JS refers to the object that is currently executing the code.
The value of 'this' depends on how a function is called.
In a method, 'this' refers to the object that the method belongs to.
In a regular function, 'this' refers to the global object (window in a browser).
In an event handler, 'this' refers to the element that triggered the event.
The value of 'this' can be explicitly set using call(), apply(), or bind() methods.
Custom Hooks are reusable functions in React that allow you to extract logic from components.
Custom Hooks are functions that start with the word 'use' and can call other Hooks if needed.
They are used to share stateful logic between components without using higher-order components or render props.
Custom Hooks can be used to handle common tasks like fetching data, managing form state, or subscribing to events.
They promote code reusability and make it easier to separate concerns...read more
A first-class function in JavaScript is a function that can be treated like any other variable.
Can be passed as an argument to other functions
Can be returned from other functions
Can be assigned to variables
Can be stored in data structures
React uses a diffing algorithm called Virtual DOM to efficiently update the actual DOM based on changes in state or props.
Virtual DOM is a lightweight copy of the actual DOM.
React compares the Virtual DOM with the previous Virtual DOM to identify the minimal number of changes needed to update the actual DOM.
This process is known as reconciliation and helps in optimizing performance by reducing unnecessary re-renders.
Example: If a component's state changes, React will update t...read more
useState() is a React hook that allows functional components to manage state.
useState() is used to declare a state variable in a functional component.
It returns an array with two elements: the current state value and a function to update the state.
The initial state value is passed as an argument to useState().
The state can be updated by calling the update function returned by useState().
useState() can be used multiple times in a component to manage multiple state variables.
Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope.
Variable declarations are hoisted to the top of their scope but not their assignments.
Function declarations are fully hoisted, meaning they can be called before they are declared.
Hoisting can lead to unexpected behavior if not understood properly.
Props are read-only properties passed from a parent component to a child component in React.
Props allow data to be passed down the component tree.
Props are immutable and cannot be modified by the child component.
Props can be used to customize the behavior or appearance of a component.
Props are accessed using the dot notation, like this.props.propName.
Props can be any type of data, including strings, numbers, objects, or functions.
Q49. How can you implement Security in React App?
Implement security in React app by using authentication, authorization, and secure coding practices.
Implement user authentication using libraries like Firebase Authentication or JSON Web Tokens (JWT).
Implement authorization by defining roles and permissions for different user types.
Use secure coding practices to prevent common vulnerabilities like Cross-Site Scripting (XSS) and Cross-Site Request Forgery (CSRF).
Implement HTTPS to ensure secure communication between the client...read more
Callbacks in JavaScript are functions passed as arguments to other functions to be executed later.
Callbacks are commonly used in event handling, asynchronous programming, and functional programming.
They allow for functions to be executed after another function has finished its execution.
Example: setTimeout(callbackFunction, 1000) will execute callbackFunction after 1 second.
Interview Questions of Similar Designations
Top Interview Questions for React Developer Related Skills
Interview experiences of popular companies
Calculate your in-hand salary
Confused about how your in-hand salary is calculated? Enter your annual salary (CTC) and get your in-hand salary
Reviews
Interviews
Salaries
Users/Month