?react Hooks - The Game Changer in Modern Web Development!
?react Hooks - The Game Changer in Modern Web Development!
useState
useEffect
useContext
useRef
useReducer
useCallback
useMemo
Custom Hooks
By Junaid Arshad
React Hooks
Hooks allow us to "hook" into
React features such as state
and lifecycle methods.
Rules:
Hooks can only be called inside React
function components.
component.
1
1. useState
To use the useState Hook, we
first need to import it into our
component.
Initialize useState
We initialize our state by calling useState
in our function component.
useState accepts an initial state and
returns two values:
The current state.
A function that updates the state.
2
Example:
3
2. useEffect
The useEffect Hook allows you
to perform side effects in your
components.
Some examples of side effects
are: fetching data, directly
updating the DOM, and timers.
useEffect accepts two
arguments. The second
argument is optional.
useEffect(<function>,
<dependency>)
4
Example:
5
3. useContext
6
Example:
7
4. useRef
8
Example:
9
5. useReducer
The useReducer Hook is similar
to the useState Hook.
It allows for custom state logic.
The reducer function contains
your custom state logic and the
initialStatecan be a simple value
but generally will contain an
object.
The useReducer Hook returns
the current stateand a
dispatchmethod.
10
Example:
11
6. useCallback
The React useCallback Hook
returns a memoized callback
function.
This allows us to isolate resource
intensive functions so that they will
not automatically run on every
render.
The useCallback Hook only runs
when one of its dependencies
update.
useCallback is a React Hook that
lets you cache a function definition
between re-renders.
12
Example:
13
7. useMemo
The React useMemo Hook
returns a memoized value.
The useMemo Hook only runs
when one of its dependencies
update.
The useMemo and useCallback
Hooks are similar. The main
difference is that useMemo
returns a memoized value and
useCallback returns a memoized
function.
14
Example:
15
8. Custom Hook
16
Example:
17
Thanks
Don’t forget Like and Follow
By Junaid Arshad