Composables with Shared State

Learn how to prevent having multiple refs that store the same information.

When useScrollPosition and useWindowSize are initialized, they create and return a new ref. This usually isn’t a problem, but there’s no need to have multiple refs that store the same information. These functionalities could be considered global to an application and can be stored just once. This can be accomplished by taking the ref state and the update method and moving them outside of the use<MethodName> composable.

Get hands-on with 1200+ tech skills courses.