Tiny Frontend Logo
Quizes 1136

On This Page

Quiz 1136: React Hooks

What are React hooks and what makes them useful?

Quick Answer

React hooks are functions that let you "hook" onto React features like state, lifecycle methods, and context from functional components. They make functional components more powerful and reusable, without needing to write class components.

Explain in Depth

React hooks were introduced in React 16.8 to provide a way to use state and lifecycle methods directly within functional components. Before hooks, these features were only available in class components.

Key Hook Functions:

  • useState: Manages state within a functional component.

    import React, { useState } from 'react';
    
    function Counter() {
      const [count, setCount] = useState(0);
      return (
        <div>
          <p>Count: {count}</p>
          <button onClick={() => setCount(count + 1)}>Increment</button>
        </div>
      );
    }
    
  • useEffect: Handles side effects like data fetching, DOM manipulation, or subscriptions.

    import React, { useState, useEffect } from 'react';
    
    function FetchData() {
      const [data, setData] = useState([]);
    
      useEffect(() => {
        fetch('https://api.example.com/data')
          .then(response => response.json())
          .then(data => setData(data));
      }, []);
    
      return (
        <div>
          {data.map(item => <p key={item.id}>{item.name}</p>)}
        </div>
      );
    }
    
  • useContext: Provides access to data from a React context.

    import React, { createContext, useContext, useState } from 'react';
    
    const ThemeContext = createContext('light');
    
    function App() {
      const [theme, setTheme] = useState('light');
      return (
        <ThemeContext.Provider value={theme}>
          <ThemeButton />
        </ThemeContext.Provider>
      );
    }
    
    function ThemeButton() {
      const theme = useContext(ThemeContext);
      return (
        <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
          Toggle Theme
        </button>
      );
    }
    

Benefits of Hooks:

  • Functional Components: Hooks enable powerful features within functional components, making them a viable alternative to class components.
  • Code Reusability: Hooks can be extracted into custom hooks for reuse across different components, promoting code modularity and reusability.
  • Simplified Logic: Hooks make it easier to manage state, lifecycle methods, and other features directly within functional components, leading to cleaner and more focused code.
  • Better State Management: Hooks help streamline state management, making it more predictable and less prone to errors.

In Summary:

React hooks are essential tools for building modern React applications with functional components. They provide a streamlined and reusable way to access core React features, leading to cleaner, more efficient, and maintainable code.

Read Next

Quiz 1156: React Portals

What are React Portals, and why are they so powerful for managing UI complexity?

What is a Prototype Chain


In JavaScript, a prototype chain is a fundamental concept that enables inheritance between objects. It's a way for an object to inherit properties and behavior from another object, allowing for code reuse and modularity.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1154: Memoization in React

Explain the purpose of React.memo and useMemo in React, and how they contribute to performance optimization.

Quiz 1153: CSS Containment contain

Describe the concept of CSS Containment and explain how it can enhance performance in web applications.

Quiz 1152: React State vs Props

What's the difference between state and props in React?