I have the function handleClick that sets my workout state variable and the useEffect that performs a post request to create a document with Mongoose. My question is: upon creating the document, mongoDB gives it an _id property; how can my workout state variable have access to that property? If I try to use setWorkout to set it, it results in an infinite loop since setWorkout would call useEffect again.
useEffect(() => {
async function postWorkout() {
const res = await axios.post('/workouts', workout)
const id = res.data._id
setWorkout(prev => ({
...prev,
_id: id
})
}
try {
postWorkout()
} catch(error) {
throw error
}
}, [workout])
const handleClick = () => {
setWorkout({
mains: mains,
accessories: accessories
})
}