React Native Slider | Tutorial for Beginners
Beto, July 7, 2023 · 9,806 views
Learn how to use sliders in React Native, focusing on beginners or intermediate developers who want to add slider functionality to their apps. You'll learn installing the community slider package, integrating it into a to-do app, and updating UI elements based on slider values.
You'll learn how to handle slider input and output, customize colors, and dynamically display emojis to represent priority levels. The tutorial uses a simple to-do app example from the Expo Router repository as a base.
What's inside
- Introduction to React Native sliders and the example app
- Why the core React Native Slider was removed and how to install the community package
- Basic slider setup with width and color customization
- Managing slider state with React useState
- Displaying the slider value and formatting it for readability
- Using slider values to update UI elements dynamically
- Showing emojis based on slider priority levels
- Creating a helper function to map slider values to emoji indexes
Introduction to React Native sliders and the example app
I start by introducing the slider component and its use case in a basic to-do application. The example app is taken from the Expo Router repository, which you can download and explore for free. The slider is intended to let users set the priority of a task (low, medium, high) visually.
This sets the stage for integrating a slider to enhance user input in the app, demonstrating practical usage rather than just theory.
Why the core React Native Slider was removed and how to install the community package
React Native removed the Slider component from its core library. Instead, you now need to install the package. I show how to find this package on the React Native Community GitHub and copy the installation command.
Using Expo, the command is run to add the dependency. After installation, the app is restarted to apply changes.
Basic slider setup with width and color customization
After installing, the slider is imported from the community package. I demonstrate setting the slider's width to 90% of the screen width for better layout. It also customizes the slider colors: the minimum track is set to blue and the maximum track to red, making the slider visually clearer.
These properties (, , with width) are essential for basic slider appearance and usability.
Managing slider state with React useState
To track the slider's value, a React state variable is created using , initialized to zero. The slider's prop is bound to this state, and the callback updates the state as the slider moves.
This setup ensures the UI reacts to user input in real time, allowing further dynamic updates based on the slider position.
Displaying the slider value and formatting it for readability
The raw slider value is a floating-point number between 0 and 1, which can have many decimal places. To make it human-readable, I show how to convert the value to a string and slice it to display only a few decimal digits.
This simple formatting improves UI clarity when showing the current slider value to users.
Using slider values to update UI elements dynamically
The slider value is used to represent task priority levels (low, medium, high). I explain how to map the continuous slider value to discrete priority states by dividing the slider range into segments.
This approach lets the app respond visually and functionally to the slider position, enhancing user experience.
Showing emojis based on slider priority levels
To visually indicate priority, I introduce an array of emojis: green circle for low, yellow circle for medium, and red circle for high priority. These emojis are displayed above the slider and update dynamically as the slider moves.
This adds a friendly and intuitive UI element that communicates priority without text.
Creating a helper function to map slider values to emoji indexes
A helper function is created to determine which emoji to show based on the slider value. It takes the slider value and the emoji array as parameters, calculates the appropriate index, and returns the corresponding emoji.
This modular approach keeps the code clean and maintainable, allowing easy adjustments if the number of priority levels changes.
Resources

CourseReact Native course
Fundamentals through shipping: the concepts behind the prompts, with lifetime access.

Premium resourcePro Membership
Get access to exclusive React Native tutorials and resources.
Like this article? Get the rest of the library plus weekly React Native tips. Free.