What is useEffect() Hook in React Native ?
useEffect
is a React Hook that allows you to perform side effects in functional components. It's widely used in both React and React Native to handle tasks such as data fetching, subscriptions, timers, or manual DOM manipulations.
Here's an overview of how to use useEffect
in React Native:
Parameters
- Effect function: This is the function where you perform your side effects.
- Cleanup function: This is optional and is used to clean up after the effect (e.g., unsubscribe or cancel a network request).
- Dependencies array: Determines when the effect should re-run.
- An empty array
[]
runs the effect only once after the component mounts. - Omitting the array runs the effect after every render.
- Including dependencies
[dependency1, dependency2]
runs the effect when one of the dependencies changes.