Skip to main content

Command Palette

Search for a command to run...

A Look At React Hooks: Introduction

Updated
A Look At React Hooks: Introduction
V

I'm a solutions engineer lead, GitHub Star, Director of WomenDevsSG, and co founder of ragTech. I work at the intersection of tech, systems, and leadership, and this blog is where I share my journey through all three. Expect honest reflections, real experiences, and thoughts that are still forming rather than polished career advice.

Welcome to A Look at React Hooks, a beginner-friendly series on React Hooks. The purpose of this series is to gather and document some awesome Hooks that are not in the official React documentation. Hopefully, by including these Hooks, more developers can know about them and use them.

First, for the beginners, we will explore some common and useful React Hooks then look at the cool custom ones I found. This article serves as a prologue/overview of this series. Let's begin!

What are hooks?

image.png Hooks are available for React packages 16.8.0 or higher. In simple words, hooks are in-built functions that allow developers to use React features such its lifecycle methods, state, props in React functional components.

The naming convention of React Hooks is useSomething such as useState, useEffect, etc. In future articles of this series, you'll see how each of these hooks work and how to use them.

Important things to know

Calling React Hooks

The most important thing to note about Hooks is that they can't be used inside class components. They can only be called in a React function component. Other situations in where Hooks are forbidden to use:

  • Inside event handlers
  • Inside functions of other Hooks like useMemo, useReducer and useEffect
  • Inside regular JavaScript functions
function Bad1() {
  function handleClick() {
    // 🔴 Bad: inside an event handler (to fix, move it outside!)
    const theme = useContext(ThemeContext);
  }
  // ...
}

function Bad2() {
  const style = useMemo(() => {
    // 🔴 Bad: inside useMemo (to fix, move it outside!)
    const theme = useContext(ThemeContext);
    return createStyle(theme);
  });
  // ...
}

class Bad3 extends React.Component {
  render() {
    // 🔴 Bad: inside a class component
    useEffect(() => {})
    // ...
  }
}

Source: https://reactjs.org/warnings/invalid-hook-call-warning.html

Must be called at the top level

When calling Hooks in function components, they must be called at the top level to ensure that they called in the same order at every render.

In other words, they:

  • Cannot be inside conditional statements
  • Cannot be in loops
  • Cannot be in functions

Because that would mean that they may or may not be executed in the same order every time the component renders.

The documentation here provides a detailed explanation on why this must be so.

All in All

In summary, learning React Hooks is not hard at all. As long as we follow its rules and understand how each of them works, we can easily use this amazing React feature in our projects.

Thanks for reading this simple introduction to Hooks. In the next article of this series, we shall take a look at our first Hook: useState. Stay tuned and cheers!

P.S. Happy Chinese New Year to any chinese readers! 新年快乐, 恭喜发财!

giphy.gif

K

It's an amazing article for beginners. I keep asking myself, why don't the actual documentations be so easy to read and understand lol.

1
J
JC Lee5y ago

Happy Chinese New year ✨✨~ Looking forward to this series 😀!

1
E

Ayy! I am super excited about this series. Woohoo! 🎉

2
V

Yayy! Thanks :)

1
F

I'm super excited for this series, Happy Chinese New Year too 🎉

1
V

Thanks Favourite! Sorry for the late reply, I was on vacation :)

1
F

No problem Victoria Lo

1
W
Wez Pyke5y ago

Hooks are for sure one of the best things about React. Happy Chinese New Year 😊

1
V

Yes, thanks a lot! :)

1
R

looking forward to this series Vic

1
V

Thanks Richard :)

1
P

Good news for me. I'm currently learning hooks... this will be of great help 🙏

1
V

Glad to hear it! Feel free to ask questions :)

1

A Look at React Hooks

Part 16 of 16

In this series, we shall look at various React Hooks and learn how we can use them to implement in our React projects! Great for beginners in React or React Hooks.

Start from the beginning

A Look At React Hooks: useSyncExternalStore

An awesome library Hook to integrate non-React state management in your apps!