.onDisappear(action: () => void): Component
A callback that runs when the component is removed from the view tree.
Support
Usage
Cleanup on removal
Text("Temporary view")
.onDisappear(() => {
console.log("View removed")
})
Stopping a timer
const body = () => {
const [count, setCount] = useState(0)
const [timerId, setTimerId] = useState(null)
return Text(String(count))
.onAppear(() => {
const id = setInterval(() => {
setCount((c) => c + 1)
}, 1000)
setTimerId(id)
})
.onDisappear(() => {
clearInterval(timerId)
})
}
Notes
- The action runs when the component is removed from the view tree, such as when a conditional hides it or when navigating away.
See Also
- onAppear — runs an action when the component first appears
- onChange — runs an action when a value changes