How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated

Photo by Vojtech Bruzek on UnsplashIn this article, we will create something similar to Medium’s app article action that appears when you scroll up and disappears when you scroll down a technique which we call “reveal on scroll” and also add a simple a…


This content originally appeared on Level Up Coding - Medium and was authored by Joseph Odunsi

Photo by Vojtech Bruzek on Unsplash

In this article, we will create something similar to Medium’s app article action that appears when you scroll up and disappears when you scroll down a technique which we call “reveal on scroll” and also add a simple animation to it. So, let’s get started.

We can now scroll, but the article action bar isn’t disappearing yet. We’ll make use of the useAnimatedScrollHandler which returns an event handler reference.

First, write the logic to check whether the user is scrolling up or down.

We defined a scrollHandler and attached it to the ScrollView.

Notice that I have changed ScrollView to Animated.ScrollView and View to Animated.View. Wrapping the component in Animatedallows the handler to trigger correctly. Also, notice the useSharedValue hook we are using to store values. The hook carries data, provides a way to react to changes, and drives animations.

Now that we can detect when the user is scrolling up or down, we can hide and show the article action bar. We will make use of the translate style property to achieve that. While the user is scrolling up, thetranslateY property should have a value of 0 and 100 when scrolling down.

There’s a new hook added, useAnimatedStyle. We’re going to use the useAnimatedStyle hook to spawn a worklet. This hook returns an object style, we can then add the style to an element. In the code above, I added the style object returned by useAnimatedStyle to the article action bar.

I also use withTiming function to achieve the animation. Without the function, the element won’t animate.

And that’s it. You can check the snackbar and the GitHub repository.

Level Up Coding

Thanks for being a part of our community! More content in the Level Up Coding publication.
Follow: Twitter, LinkedIn, Newsletter
Level Up is transforming tech recruiting ➡️ Join our talent collective


How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Joseph Odunsi


Print Share Comment Cite Upload Translate Updates
APA

Joseph Odunsi | Sciencx (2022-07-11T00:13:04+00:00) How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated. Retrieved from https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/

MLA
" » How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated." Joseph Odunsi | Sciencx - Monday July 11, 2022, https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/
HARVARD
Joseph Odunsi | Sciencx Monday July 11, 2022 » How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated., viewed ,<https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/>
VANCOUVER
Joseph Odunsi | Sciencx - » How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/
CHICAGO
" » How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated." Joseph Odunsi | Sciencx - Accessed . https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/
IEEE
" » How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated." Joseph Odunsi | Sciencx [Online]. Available: https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/. [Accessed: ]
rf:citation
» How to Show and Hide Content Scrolling Up and Down in React Native with Reanimated | Joseph Odunsi | Sciencx | https://www.scien.cx/2022/07/11/how-to-show-and-hide-content-scrolling-up-and-down-in-react-native-with-reanimated/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.