Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

React Hooks

React Hooks are functions that let you use state and other React features without writing a class. They were introduced in React 16.8 to simplify and streamline React component code by allowing developers to manage state and side effects in functional components.

React Hooks

1. useState

Manages state in functional components.

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

2. useEffect

Handles side effects in functional components, such as fetching data or directly interacting with the DOM.

useEffect(() => {
  document.title = `You clicked ${count} times`;
}, [count]);

3. useContext

Provides a way to pass data through the component tree without having to pass props down manually at every level.

const value = useContext(MyContext);

4. useReducer

An alternative to useState for managing more complex state logic.

const [state, dispatch] = useReducer(reducer, initialState);

5. useCallback

Memoizes a function, preventing it from being recreated on every render.

const memoizedCallback = useCallback(() => {
  doSomething(a, b);
}, [a, b]);

6. useMemo

Memoizes a value, recomputing it only when its dependencies change.

const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);

7. useRef

Creates a mutable object that persists for the lifetime of the component, often used to reference DOM elements.

const myRef = useRef(null);

8. useImperativeHandle

Customizes the instance value that is exposed when using ref.

useImperativeHandle(ref, () => ({
  customFunction() {
    // Custom function logic
  }
}));

9. useLayoutEffect

Similar to useEffect, but fires synchronously after all DOM mutations. Useful for measuring DOM nodes.

useLayoutEffect(() => {
  // Code here
}, []);

Contributing

Feel free to submit a pull request or open an issue if you find any issues or have suggestions for improvements.

About

This repository is a set of all React hooks which are used frequently.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages