0
const [data, setData] = useState(undefined);
useEffect(() => {
    if (changeRequests.data) {
        setData(setDefaultState(changeRequests.data));
    }
}, [changeRequests.data, setData,setDefaultState ]);

const setDefaultState = data => {
    let names = getNameList(data);
    setSelectedItems(names);
}

The 'setDefaultState' function makes the dependencies of useEffect Hook (at line 78) change on every render. Move it inside the useEffect callback. Alternatively, wrap the 'setDefaultState' definition into its own useCallback() Hook

const [resultsFallback] = useState([]);
const getNameList = res => {
let jList = getJData(res);
let jNames = resultsFallback;
jNames = jList.map(j => {
    return `${j.joggers}`
});
return jNames;

}

The 'getNameList' function makes the dependencies of useCallback Hook change on every render. To fix this, wrap the 'getNameList' definition into its own useCallback() Hook

2
  • you can include the setDefaultState in the list of dependencies which will make the warning go away Commented Apr 11, 2020 at 14:23
  • If setDefaultState isn't used elsewhere you can move it into the useEffect function or do it how Barry suggested Commented Apr 11, 2020 at 15:41

2 Answers 2

1

Change your setDefaultState declaration to this:

const setDefaultState = useCallback((data) => {
  let names = getNameList(data);
  setSelectedItems(names);
}, [getNameList, setSelectedNames])

Don't forget to import React, { ..., useCallback } from 'react'

The 'getNameList' function makes the dependencies of useCallback Hook change on every render. To fix this, wrap the 'getNameList' definition into its own useCallback() Hook

Sign up to request clarification or add additional context in comments.

Comments

0

For error 1):The linter is telling you that you're depending on external values that may change,You can tell React to skip applying an effect if certain values haven’t changed between re-renders.If your array does not change the value, it will remain the same. 2)data should be used inside the component, here you are passing it as a argument, data should be inside the JSX expression and after re rendering it will change the value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.