Learn React Basics: React Hooks

Explore the essentials of React Hooks with practical examples and latest best practices, perfect for enhancing your JavaScript and React skills.

Β·

3 min read

Learn React Basics: React Hooks

Here we go another React Hooks tutorial :). I definitely created the first one of these on the internet (cough cough sarcasm).

React Hooks, introduced in React 16.8, drastically changed the way we write React components. They allow you to use state and other React features in functional components, which was previously only possible in class components. This makes code more readable and easier to maintain.

Today, we'll explore two key hooks: useState and useEffect, with examples and documentation links. These are by far the most common hooks out there. By no means do they mark the complete universe of hooks, but you can get pretty far in the React universe with just these two.

Understanding useState

The useState hook is the cornerstone for managing state in functional components. It's a simpler and more elegant way to introduce state in your components without converting them into classes (which for all intents and purposes are dead in React at this point).

Example: A Simple Counter

Here's a basic example that demonstrates the useState hook:

import { useState } from 'react';

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

  return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>
        Click me
      </button>
    </div>
  );
}

export default Counter;

In this example, useState(0) initializes the count state variable to 0. setCount is a function that updates count. When you click the button, setCount is called with the new count value, triggering a re-render with the updated state.

Note the automatic re-render where React will re-render the page to show the updated state. There is some performance overhead with this operation.

For more details on useState, check the official documentation.

Exploring useEffect

The useEffect hook is used for performing side effects in function components. It's similar to lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount in class components.

Example: Updating Document Title

Let's look at an example where useEffect is used to update the document title:

import { useState, useEffect } from 'react';

function TitleUpdater() {
  const [count, setCount] = useState(0);

  useEffect(() => {
    // Update the document title using the browser API
    document.title = `You clicked ${count} times`;
  });

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

export default TitleUpdater;

In this example, useEffect is used to update the document's title every time the component renders. The hook runs after every render by default, including the first render.

For an in-depth understanding of useEffect, refer to the React documentation.

Conclusion

React Hooks offer a more intuitive and functional approach to building components. By understanding and utilizing hooks like useState and useEffect, you can write more concise and maintainable React applications. They not only simplify your component logic but also pave the way for better state management and side-effect handling in functional components.

For further reading and advanced hook patterns, the React documentation provides comprehensive guides and examples. Happy coding!

Did you find this article valuable?

Support Sean Coughlin by becoming a sponsor. Any amount is appreciated!

Β