Skip to content
Discussion options

You must be logged in to vote

This is actually the intended behavior in React 18 when you are in development mode!

React automatically wraps your application in <React.StrictMode>. In development, Strict Mode unmounts and remounts every component automatically whenever it is rendered for the first time. This is done on purpose to help you find bugs related to side effects and to ensure your useEffect cleanup functions are working properly.

How to handle it:

Option 1 (Recommended): Add a cleanup function Make sure your effect cleans up after itself (e.g., aborting a fetch request):

javascript
useEffect(() => {
const controller = new AbortController();

fetch('/api/data', { signal: controller.signal })
.then(res => res.j…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@icyJoseph
Comment options

Answer selected by rohan0maske-sys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants