Skip to content

Instantly share code, notes, and snippets.

@atNeerajShukla
Last active July 31, 2024 10:39
Show Gist options
  • Save atNeerajShukla/63d47c89f3434d28d8acf99e9e6ef30b to your computer and use it in GitHub Desktop.
Save atNeerajShukla/63d47c89f3434d28d8acf99e9e6ef30b to your computer and use it in GitHub Desktop.

React Hooks Explained for Beginners

React Hooks are special functions that let you use state and other React features without writing a class. They simplify code and enhance functionality.

The most commonly used hooks are:

  1. useState The useState Hook lets you add state to functional components. It returns an array with two elements: the current state and a function to update it.

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

  1. useEffect The useEffect Hook allows you to perform side effects in your components, such as data fetching, subscriptions, or manually changing the DOM. By default, it runs after every render, but you can control when it runs by specifying dependencies.

useEffect(() => { document.title = useEffect Hook; }, [count]);

  1. useContext The useContext Hook allows you to access the value of a context directly in your functional components. This is useful for accessing global data like themes, user settings, or authentication status without passing props down manually.

const theme = useContext(ThemeContext);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment