Answer the question
In order to leave comments, you need to log in
Why does it require 'dispatch' to be installed as a dependency?
Gives a warning to the console: Line 14:6: React Hook React.useEffect has a missing dependency: 'dispatch'. Either include it or remove the dependency array react-hooks/exhaustive-deps. Why am I being asked to set dispatch to the useEffect dependency?
import * as React from 'react'
import css from './List.module.css';
import Note from '../Note/Note'
import { useDispatch, useSelector } from 'react-redux'
import { setTotal } from '../../Redux/actions/note'
export const List = () => {
const notes = useSelector(({ note }) => note.items)
const dispatch = useDispatch()
React.useEffect(() => {
dispatch(setTotal(notes.length))
}, [notes])
return (
<>
<ul className={css.list}>
{notes && notes.map((note, i) => <Note i={i} key={`key_${i}`} text={note}/>)}
</ul>
</>
)
}
export default List;
Answer the question
In order to leave comments, you need to log in
Because you are using this method inside useEffect.
Add Method to Dependencies
React.useEffect(() => {
dispatch(setTotal(notes.length))
}, [notes, dispatch])
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question