Turns out that if you want to call a function that makes use of props or state inside the body of the function you need to call it using a useEffect that listens for the required variables and call the function again after the variables are updated.
For example, take dispatchSweetAlert
which is a function that accepts a alertConfig
object containing title
, onConfirm
, and onCancel
. When you call dispatchSweetAlert
the onConfirm
function will execute with the values that were present at that point in time when dispatchSweetAlert
was called. If something changes after the alert is shown then you would need to call dispatchSweetAlert
again in order for the onConfirm
function to be called with the updated values.
For this you can use a useEffect that checks if the alert is open. If so, update the alertConfig
so that the onConfirm
runs with the latest changes as determined by the useEffect
.
Top comments (0)