Common React Hook Questions

Answering common questions about React hooks and providing some tips for working with them, hope you find this helpful!
Saturday, March 2, 2024

What are React hooks

React hooks are a way for developers to separate logic from the UI in React applications in a way that is flexible and reusable. They allow developers to write a custom hook that encapsulates stateful logic and then use that hook in multiple components. This makes it easier to share logic between components and reduces the amount of code that needs to be written.

How do React hooks work

Under the hood, React hooks work essentially via a very complex combination of closures, function calls and the React reconciler. When you call a hook what typically happens is that React will create a new instance of the hook and store it in a global registry. Then, when the component is rendered, React will call the hook and retrieve the state and other values that it needs. This allows the hook to be reused across multiple components and ensures that the state is preserved between renders.

What hooks does React ship with

React comes out of the box with a series of hooks intended for different purposes and use cases, they are:

  • useState: A hook for managing state (locally on a component level)
  • useEffect: A hook for managing side effects in functional components
  • useContext: A hook for accessing the React context API (global application state)
  • useReducer: A hook for managing state with a reducer function
  • useCallback: A hook for memoizing functions
  • useMemo: A hook for memoizing values
  • useRef: A hook for creating a mutable reference to a DOM element or a value
  • useImperativeHandle: A hook for exposing imperative methods to parent components
  • useLayoutEffect: A hook for managing side effects that require access to the DOM
  • useDebugValue: A hook for displaying custom labels in the React DevTools

Are React hooks async

By themselves react hooks are not async. But you can use async functions inside a hook. You can also use the new use hook to handle async operations within a custom hook, alternatively you can use the useEffect hook to handle async operations.

What are React hooks used for

React hooks can be used for pretty much any separation of logic and UI in a React application. Optimally you should use hooks to separate reusable logic from the UI, and to make it easier to share stateful logic between components. So essentially you may have a hook for handling API calls to a specific service so that you do not have to repeat the same calls in multiple components. This makes debugging and maintaining the code easier.

Can a React hook return a component

In short yes, React hooks can return components or any other value. However it is not recommended to return a component from a hook, since hooks generally should be used to separate logic and UI. But there are exceptions to every rule but do ask your self if you couldn’t refactor that better so you wouldn’t need to return a component from a hook.

Can a React hook return a promise

Technically yes, a hook can return a promise. But it is generally not recommended to return a promise from a hook, since hooks are intended to be synchronous. If you need to handle async operations in a hook, you should use the useEffect hook or the new use hook. But since we got RSC(React Service components) you can use a hook to return a promise from a service component and then resolve it in a react server component.

Can a React hook return a function

Yes, a hook can return a function. This is actually a common use case for hooks, since they are often used to encapsulate stateful logic. For example, you might have a hook that returns a function for handling API calls or for updating a piece of state.

Can React hooks replace Redux or other state management libraries

If you got enough time on your hands, yes. Many of the featured of Redux, Zustand, Jotai etc. utilise a combination of hooks and the Context API under the hood. So you can essentially make your own State management library by using hooks and the Context API. But do you have more resources and time than the Redux, zustand, jotai team? if not then maybe save yourself the hassle and pick one of the fantastic libraries out there.

Can React hooks be used for API calls

Yes, that is a common use case for hooks, you can use the useEffect hook to handle API calls in a functional component. You can also create a custom hook that encapsulates the logic for making API calls and then use that hook in multiple components. This makes it easier to share the logic for making API calls between components and reduces the amount of code that needs to be written.

I personally like to have a service/ folder where I store my API calls and then a useService that consumes the service and returns the data. This makes it easier to maintain and debug the code, plus it lets me nicely separate data that belongs in RSCs(React Service Components) and data that belongs in client side components.

Can React Server Compoents use hooks

Yes and no. React Server Components can not use hooks that rely on client side stuff like useState, useEffect, useRef etc. But you can use custom hooks within react server components no problem.

Can React hooks be called conditionally

You can but don’t! React hooks should always be called in on the top level of components. Calling hooks conditionally can lead to bugs and unexpected behavior.

Can React hooks be used with React Native

Yes, React hooks can be used with React Native. In fact, hooks are a great fit for React Native, since they allow you to separate logic from the UI in a way that is flexible and reusable. You can use the same hooks that you would use in a web application, such as useState, useEffect, useContext, etc. One minor difference is that you can’t use the useLayoutEffect hook in React Native, since it relies on the DOM. But you can use the useEffect hook instead.

Additionally React Native may recieve some built in hooks a bit later than React, such as the new use hook.

Can React hooks take in props

Yes, a hook can take in props. This is a common use case for hooks, since they are often used to encapsulate stateful logic. For example, you might have a hook that takes in a prop and returns a function for handling that prop.

example:

const useCounter = (initialValue) => {
  const [count, setCount] = useState(initialValue);
  const increment = () => setCount(count + 1);
  const decrement = () => setCount(count - 1);
  return { count, increment, decrement };
};

React hooks and singleton pattern

React hooks are not a singleton pattern, but they can be used to create a singleton pattern. You can create a custom hook that encapsulates the logic for creating a singleton instance and then use that hook in multiple components. This makes it easier to share the logic for creating a singleton instance between components and reduces the amount of code that needs to be written.

Conclusion

React hooks are a powerful tool for managing state and side effects in React applications. They can be used to simplify complex logic and make it easier to share stateful logic between components. However, they can also be confusing to work with, especially for developers who are new to React. In this article, I’ve answered some common questions about React hooks and provided some tips for working with them.

Keep reading

If you liked that one here's another:
react useDebounce

Wanna show support?

If you find my sporadic thoughts and ramblings helpful.
You can buy me a coffee if you feel like it.
It's not necessary but it's always appreciated. The content will always stay free regardless.