Cognizant
TCS Interview Questions and Answers
Q1. 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
Q2. 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.
Q3. 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
Q4. 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
Q5. How componentWillUnmount works?
componentWillUnmount is a lifecycle method in React that is called right before a component is unmounted and destroyed.
componentWillUnmount is used to perform any necessary cleanup tasks before a component is removed from the DOM.
It is commonly used to cancel any pending network requests, remove event listeners, or clear timers or intervals.
The method is called automatically by React when a component is about to be unmounted.
It is important to clean up any resources or subscr...read more
Q6. Explain useState, useEffect Hooks.
useState and useEffect are React Hooks used for managing state and side effects respectively.
useState is used to manage state in functional components
It returns an array with two elements - the current state value and a function to update the state
useEffect is used to manage side effects like fetching data or updating the DOM
It takes a function as its argument and runs it after every render
useEffect can also take a second argument which is an array of dependencies to control ...read more
More about working at Cognizant
Top React Developer Interview Questions from Similar Companies
Reviews
Interviews
Salaries
Users/Month