Add scoped function

Add a function to handle the common enter-leave pattern.