Today’s Offer Enroll today and get access to premium content.
App Store Google Play

React JS Interview Questions

React JS is a JavaScript library developed by Facebook for building dynamic, component-based user interfaces with a virtual DOM for optimal performance.

Showing 5 of 5

In React, keys help identify which items in a list have changed, been added, or removed. They are essential for optimizing the rendering process.

When rendering lists of elements, React uses keys to keep track of each element's identity. This allows React to minimize re-renders by efficiently updating only the components that have changed.

To implement keys, ensure that each list item has a unique identifier. Here's an example:

const items = ['Apple', 'Banana', 'Cherry'];

const ItemList = () => (
  
    {items.map((item, index) => (
  • {item}
  • ))}
);
Open

State management in React allows components to maintain and manage their own data. State is a built-in object that stores dynamic data about the component.

To manage state, you can use the useState hook in functional components. Here's a step-by-step guide on how to do this:

  1. Import useState from React.
  2. Initialize state using the useState hook.
  3. Update state using the function returned by useState.

Here's an example:

import React, { useState } from 'react';

const Counter = () => {
  const [count, setCount] = useState(0);

  return (
    

Count: {count}

); };
Open

React hooks are functions that let you use state and other React features without writing a class. They simplify component logic and enhance reusability.

Hooks such as useState and useEffect allow functional components to manage state and lifecycle events. This helps in writing cleaner and more maintainable code.

For instance, using useEffect to perform side effects:

import React, { useEffect } from 'react';

const Timer = () => {
  useEffect(() => {
    const timer = setInterval(() => console.log('Tick'), 1000);
    return () => clearInterval(timer);
  }, []);

  return 

Timer is running. Check the console!

; };
Open

In React, controlled components are those whose value is controlled by React state. Uncontrolled components manage their own state internally.

In a controlled component, form data is handled by the state in React, making it easier to manipulate and validate data. In contrast, uncontrolled components rely on the DOM to manage their state.

Here's an example of a controlled component:

import React, { useState } from 'react';

const ControlledInput = () => {
  const [value, setValue] = useState('');

  return  setValue(e.target.value)} />;
};
Open

The React Context API provides a way to share values between components without having to pass props through every level of the component tree.

This is particularly useful for global data such as themes or user authentication. You create a context, provide a value, and consume it in any component that needs it.

Here’s how to implement it:

import React, { createContext, useContext } from 'react';

const ThemeContext = createContext();

const App = () => (
  
    
  
);

const Toolbar = () => {
  const theme = useContext(ThemeContext);
  return 
Toolbar
; };
Open