Introduction to React: A Comprehensive Overview

Understand React principles, hooks, and features to confidently tackle interview questions.

Overview

React is a popular JavaScript library for building user interfaces, particularly single-page applications. By componentizing UI elements, React enhances reusability and maintainability, which are essential in modern web development. Understanding React is vital for developers, as it forms the backbone of frameworks like React Native and Next.js, commonly integrating essential web development concepts.

How it works

At its core, React uses a component-based architecture, which allows developers to build encapsulated components that manage their own state. State and props are fundamental concepts in React — state represents dynamic data specific to a component, while props are used to pass data down to child components. Below is a simple functional component demonstrating state and props:

import React, { useState } from 'react';

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

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={() => setCount(count + 1)}>Increment</button>
    </div>
  );
};

export default Counter;

This example shows how to create a counter with an initial state. The useState hook is used to handle component state efficiently.

Concept Description
React Native Reuses React components for mobile applications.
use callback Returns a memoized version of the callback that only changes if one of the dependencies has changed.
use effect Runs after every render and can be used for side effects. By default, it runs after every render.
Keys in Lists A stable key helps React identify which items have changed, are added, or are removed, improving performance.
React 18 Updates Multiple setState calls within the same event handler trigger only one re-render.
useMemo Primarily solves performance problems by memoizing expensive calculations.

Common Mistakes

  • Forgetting to return the cleanup function in useEffect, which can lead to memory leaks.
  • Not using unique and stable keys for lists, causing unexpected behavior during updates.
  • Overusing state and not leveraging props for passing data, which can lead to unnecessary complexity.
  • Confusing functional components and class components, especially regarding lifecycle methods.
  • Misunderstanding the reference equality of functions created by useCallback, which may cause unwanted re-renders.

FAQ

Q: What does the "use client" directive do? A: It specifies that the component must render on the client side, essential for components relying on browser APIs or states.

Q: In the Next.js App Router, components are by default: A: Server components, which means they render on the server by default for improved performance and SEO.

Q: Why should list items have a stable key? A: Stable keys help React optimize rendering by tracking which items have changed, ensuring efficient updates and reduced re-renders.

Q: What does useCallback return? A: It returns a memoized callback function that only changes if its dependency array changes, helping prevent unnecessary re-renders in child components.

References

Practice

Ready to practice React?

Answer real questions, get instant feedback, and watch your skill score climb — free. Practice is in English, like real tech interviews.

Try one 👇

ReactHooksMid
0 XP
When does useEffect run by default?

↑ Go ahead — pick an answer. This is Skillpato.