Understanding useEffect design
React provides a useEffect
hook to set up a callback to be invoked after an update:
const Title = () => { useEffect(() => { window.title = "Hello World" return () => { window.title = "Notitle" } }, []) }
The useEffect
function takes a callback function called create
as its first input argument to define the effect. In the preceding example, the effect sets the window.title
to be Hello World
when the component is mounted.
A create
function can return a function called destroy
to perform the cleanup. The interesting thing here is that the destroy
function is provided by the create
function as a return value. In the preceding example, the cleanup reverts the window.title
object back to NoTitle
when it is unmounted.
The second parameter in the useEffect
argument list is a dependency array...