Exploring React Hooks : Prologue

Exploring React Hooks : Prologue

Hello and welcome to Exploring React Hooks, an introductory series on React Hooks.

Throughout this series, we will explore various hooks provided by React, exploring their functionalities, use cases, and recommended practices. Each blog post will focus on a specific hook, uncovering its capabilities and demonstrating how it can improve your React development experience.

So the first question that inevitably pops up, is..

What are Hooks?

Hooks are the functions that "hook into" React state and lifecycle features from functional components.

React Hooks revolutionized the way we write React components by providing a simpler and more elegant alternative to class components. They allow us to use various class-based component features such as state and other React features without using a class-based syntax.

Hooks were added to React in version 16.8.

Naming Convention: Hooks are typically named with a prefix of "use", followed by a descriptive name that reflects the purpose or behavior of the hook. For example: useState, useEffect, useContext, etc.

In short, Hooks allow functional components to have access to state and other React features, enabling us to build complex UIs with less code and greater flexibility.

Hook Rules

1) Call Hooks at the Top Level

Don’t call Hooks inside loops, conditions, or nested functions.

You should always use Hooks at the top level of your React function. By following this rule, you ensure that Hooks are called in the same order every time a component is rendered.

2) Call Hooks from React Functions

Don't call Hooks from regular JavaScript functions.

You should only:

  • Call Hooks from React function components.

  • Call Hooks from Custom Hooks.

Advantages of Hooks:

🪝Simplifying State Management:

In the past, managing state in React components involved writing class components with complex lifecycle methods. This approach often resulted in code duplication and made it harder to separate concerns. However, the introduction of React Hooks changed the way we handled state by allowing us to manage state directly inside functional components, eliminating the need for classes.

With hooks like useState, we can easily create and update state variables directly within our functional components, making our code more readable and maintainable.

🪝Effortless Side Effects:

In class components, side effects, such as fetching data from an API or setting up a subscription, were previously managed through different lifecycle methods like componentDidMount, componentDidUpdate and componentWillUnmount. However, hooks like useEffect make handling side effects simple to use in functional components.

We'll explore the useEffect hook in detail in one of the upcoming blog posts.

🪝Custom Hooks:

With React Hooks, we have the ability to create our own custom hooks, that encapsulate reusable logic and minimize redundancy. By utilizing custom hooks, we can eliminate the need for lifecycle methods and the use of 'this' keyword, resulting in cleaner code and improved performance compared to class components.

We'll explore the process of building custom hooks that can be shared across multiple components later on in the series.

Conclusion

In this introductory overview, we've established the groundwork for our Exploring React Hooks series.

We've covered the fundamental concepts of React Hooks, including its rules, and advantages.

In the upcoming blogs, we'll analyze individual hooks, providing detailed explanations and practical examples for each of them.


References


Thank you for taking the time to read this blog. I hope you found it informative and enjoyable!

Share your thoughts and feel free to connect with me on Twitter.

Stay tuned for the first article of this series, where we shall take a look at our first built-in React Hook: - useState Hook.

Catch you guys on the next one. Cheers .. ✌️

Did you find this article valuable?

Support Raj Sarkar by becoming a sponsor. Any amount is appreciated!