Usedispatch() inside a useEffect() -> *Missing dependency*

Asked

Viewed 970 times

0

ALERT: React Hook useEffect has a Missing dependency: 'Dispatcher'. Either include it or remove the dependency array React-Hooks/exhaustive-deps

import React, {useEffect} from 'react'
import {useDispatch, useSelector} from 'react-redux'
import ACTION from './Action'

const App = () => {
  
  const dispatch = useDispatch();
  const REDUCER_USUARIOS = useSelector(state => state.ReducerUsuarios)
  
  //Estou usando como componentDidMount()
  useEffect(() => {  
   dispatch(ACTION.CARREGAR())
  }, [])
  
  console.log(REDUCER_USUARIOS)
  
  return <div>TEST</div>

}

NOTE: I am using React(version:16.12) and React-Redux(version:7.1.3) with Hooks.

1 answer

2

The alert is given because the Dispatch function is not in the useEffect dependency array. To solve need to leave this way

 useEffect(() => {  
    dispatch(ACTION.CARREGAR())
 }, [dispatch])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.