Home  >  Article  >  Web Front-end  >  Prop not updating inside function

Prop not updating inside function

王林
王林Original
2024-08-24 11:08:06950browse

Prop not updating inside function

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 calls 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.

The above is the detailed content of Prop not updating inside function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn