0

I try to use useEffect but it gets error looks like in below,

React Hook useEffect has a missing dependency: 'data'. Either include it or remove the dependency array

Here is my component,

let id = props.location.pathname.split("--")[1];
let str = props.location.pathname.split("--")[0].substr(1);
const data = {id: id, link: str}

const [title, setTitle] = useState("")

useEffect(() => {
    setTitle("...") // Yükleniyor.
    async function getTitle() {             
        axios.post('/api/data/entry/get', data)
        .then(res => {
        setTitle(res.data.title)
        // TODO: Catch ekle.
        })
    }
    getTitle()
}, [props])
mehmetdemiray
  • 976
  • 4
  • 13
  • 32
  • The best way for you to write your code would be to pass id and str as deps to useEffect and form the data object within the useEffect callback – Shubham Khatri Nov 08 '19 at 08:11

2 Answers2

0

You have to include “data” in the dependency array. This is because your hook uses it inside its callback.

This way, the hook will be called every time one of the variables in the dependency array is changed.

I noticed the “data” object uses values in the properties of the component. You might say “ok then why should I include both props and data?” Well, when defining a dependency array, you need to be as granular as possible. Having it depend on props is way too general. In your case, you should make it depend on nothing but “data”

EDIT

I missed the fact that if you were to add data as a dependency, then the hook would trigger with every re-render. That's because data is a new object with every render, basically. You could separate the members of data into variables and use those as dependencies:

Your component would now look like this:

const id = props.location.pathname.split("--")[1];
const str = props.location.pathname.split("--")[0].substr(1);

const data = useRef({id: id, link: str});

const [title, setTitle] = useState("")

useEffect(() => { /* ... */ }, [id, str]);

Note that I have not tested the code. Please see if this works.

Victor
  • 13,914
  • 19
  • 78
  • 147
  • Thanks. But if i don't add props to dependency, useEffect doesn't work every route changes. I tried. and when i add data to dependencies, it posts data every second to server automatically. – mehmetdemiray Nov 08 '19 at 07:51
  • @MehmetDemiray that's because `data` is a new object everytime your component renders. How about you use `props.location` instead as a dependency? – Victor Nov 08 '19 at 08:13
0

You have to add data to your dependency list, like below

let id = props.location.pathname.split("--")[1];
let str = props.location.pathname.split("--")[0].substr(1);
const data = {id: id, link: str}

const [title, setTitle] = useState("")

useEffect(() => {
    setTitle("...") // Yükleniyor.
    const getTitle = async () => {
      const res = await 
        axios.post('/api/data/entry/get', data)
        setTitle(res.data.title)
        // TODO: Catch ekle.
        };
    getTitle()
}, [props])
akano1
  • 40,596
  • 19
  • 54
  • 67