Hello guys, It’s been almost a week since I had posted a tutorial. So now to wait is over. In today’s tutorial we would learn about a react native’s NPM library @react-native-community/slider. The @react-native-community/slider library is used to create simple slider, trackbar, seek bar in react native. The main function of slider is to increase or decrease value on a fixed parameter when the slider moves forward or backward. So in this tutorial we would learn about Example of React Native Community Slider in Android iOS Application. You can also visit its official NPM page here.
Contents in this project Example of React Native Community Slider in Android iOS :-
1. First of all we have to install the @react-native-community/slider NPM package in our react native project. So open your react native project Root directory in CMD in windows and in Terminal in MAC and execute below command.
1
|
npm install @react–native–community/slider —save
|
Screenshot :-
Screenshot on finish installation :-
2. Now in the next step we would start coding for the application. So open your project’s main App.js file and import View, StyleSheet, Text and useState component.
1
2
3
|
import React, { useState } from ‘react’;
import { View, StyleSheet, Text } from ‘react-native’;
|
3. Import Slider component from ‘@react-native-community/slider’ .
1
|
import Slider from ‘@react-native-community/slider’;
|
4. Creating our main Export default functional component App.
1
2
3
4
5
|
export default function App() {
}
|
5. Creating a State named as data with State change function setSliderData. Here we would set the default data value to 10. This would make the Slider default value of 10.
1
|
const [data, setSliderData] = useState(10);
|
6. We would make 1 Text component in the return() block of code, In this text we would display the Slider data state value.
1
2
3
|
<Text style={{ fontSize: 28 }}>
Value of slider is : {data}
</Text>
|
7. Now we would make the Slider component. I’m using most of Slider Props in our current example, But there are more of them, You can read about the remaining Props here on the package page.
- maximumValue : To define maximum value of a Slider.
- minimumValue : Minimum value of slider, From which value the slider should start.
- minimumTrackTintColor : This is the color of Slider which is moved.
- maximumTrackTintColor : The color of Slider which is remaining after moving.
- step : The value should be increased like +1 .
- value : Value of Slider.
- onValueChange : Calls whenever value is changed of Slider.
- thumbTintColor : Slider moving round button color.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<Slider
maximumValue={100}
minimumValue={0}
minimumTrackTintColor=“#D50000”
maximumTrackTintColor=“#01579B”
step={1}
value={data}
onValueChange={
(sliderValue) => setSliderData(sliderValue)
}
thumbTintColor=“#1B5E20”
style={{width: 400, height: 40}}
/>
|
8. Creating style.
1
2
3
4
5
6
7
8
9
|
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
padding: 10,
justifyContent: ‘center’,
alignItems: ‘center’
}
});
|
9. Complete Source Code for App.js file :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
import React, { useState } from ‘react’;
import { View, StyleSheet, Text } from ‘react-native’;
import Slider from ‘@react-native-community/slider’;
export default function App() {
const [data, setSliderData] = useState(10);
return (
<View style={styleSheet.MainContainer}>
<Text style={{ color: ‘blue’, fontSize: 30, textAlign: ‘center’ }}>
Example of React Native Community/Slider
</Text>
<Text style={{ fontSize: 28 }}>
Value of slider is : {data}
</Text>
<Slider
maximumValue={100}
minimumValue={0}
minimumTrackTintColor=“#D50000”
maximumTrackTintColor=“#01579B”
step={1}
value={data}
onValueChange={
(sliderValue) => setSliderData(sliderValue)
}
thumbTintColor=“#1B5E20”
style={{width: 400, height: 40}}
/>
</View>
);
}
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
padding: 10,
justifyContent: ‘center’,
alignItems: ‘center’
}
});
|
Screenshots :-