DEV Community

SheetsC
SheetsC

Posted on

Flatiron Phase-2

3-3-23:
Its Friday, and here at flatirons 1-30SE East cohort its Blog time!! Aside from it being required, I have heard first-hand that these can be extremely helpful in our career endeavors.
This blog post in particular (if my understanding is correct) is about the usefulness of the dependency array for the useEffect hook. I have found myself needing to iterate over the array of objects fetched based on their assigned IDs. This became an issue when the non deleted objects ID's were not reassigned in the array (4,5,6,7 and 6 is deleted now the array is 4,5,7 and not 4,5,new6). A way around this is to use the dependency array for the useEffect's second argument that refers to the state of the array you are filling with the data fetched.

const[arrayState, setArrayState] = useState([])

useEffect(()=>{fetch('api')},[arrayState])

this way, when arrayState changes the objects will be refetched. This could even be utilized when sorting data to have the order of objects in the array is reflected in the ID's as well

Top comments (0)