J
J
justifycontent2021-06-12 01:12:36
React
justifycontent, 2021-06-12 01:12:36

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

1 answer(s)
S
Sergey Suntsev, 2021-06-12
@justifycontent

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 question

Ask a Question

731 491 924 answers to any question