TW
← Back to writing

Writing

Getting Started with React Hooks

Tallha WaheedNovember 15, 20242 min read

Learn the basics of React Hooks and how to use them in your functional components.

Getting Started with React Hooks

React Hooks have revolutionized the way we write React components. In this post, we'll explore the fundamentals of Hooks and how they can simplify your code.

What are React Hooks?

Hooks are functions that let you "hook into" React state and lifecycle features from function components. They don't work inside classes — they let you use React without classes.

The useState Hook

The useState Hook is one of the most commonly used hooks. It allows you to add state to functional components:

import { useState } from 'react';

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

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

The useEffect Hook

The useEffect Hook lets you perform side effects in function components. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount combined.

import { useEffect, useState } from 'react';

function DataFetcher() {
  const [data, setData] = useState(null);

  useEffect(() => {
    fetch('/api/data')
      .then(res => res.json())
      .then(data => setData(data));
  }, []);

  return <div>{data && <pre>{JSON.stringify(data, null, 2)}</pre>}</div>;
}

Benefits of Hooks

  • Easier code reuse: Extract stateful logic and reuse it across components
  • Simpler components: Break complex components into smaller, manageable pieces
  • Better performance: Optimize rendering by specifying dependencies

Conclusion

React Hooks provide a powerful way to write cleaner, more maintainable React code. Start with useState and useEffect, and explore other hooks as you become more comfortable.

Happy coding!