Hooks API Reference
Frontend Hamroun provides a complete hooks API that mirrors React's core hooks with additional performance optimizations.
useState
Description
useState is a Hook that lets you add state to your function components. It returns a stateful value and a function to update it.
Syntax
Example Code
Try It Live
You clicked 0 times
Best Practices
Do
- Use functional updates for state that depends on previous state
- Group related state with objects or use multiple state variables
Don't
- Modify state directly without using the setter function
- Rely on previous state without using functional updates
Usage Metrics
Custom Hooks
Custom Hooks let you extract component logic into reusable functions. Here are some useful custom hooks you can use in your Frontend Hamroun applications:
useTimeout
A hook for handling setTimeout with automatic cleanup
useLocalStorage
A hook for persisting state in localStorage
useOnScreen
A hook for detecting when an element is visible on screen
useFetch
A hook for handling API requests with loading and error states
Hooks FAQ
Use useState for simple state management and useReducer for complex state logic, especially when state transitions depend on the previous state or when multiple sub-values are related.
In development mode with React.StrictMode enabled, useEffect runs twice to help detect side effects that need cleanup. This only happens in development, not production.
No, hooks should always be called at the top level of your components, not inside conditionals, loops, or nested functions. This ensures the hooks are called in the same order each time.
useMemo memoizes a computed value, while useCallback memoizes a function. Use useMemo when you want to avoid recalculating expensive values, and useCallback when you want to prevent function recreation that might cause child components to re-render unnecessarily.
Next Steps
Now that you've learned about Frontend Hamroun's hooks API, explore these related resources: