React Native launched its new Pressable component with version 0.63 Stable. The Pressable component is used to detect various stages of click events like onPress, onPressIn and onPressOut. Using then Pressable component we can easily change Button layout or Style on a particular event. So in this tutorial we would learn about React Native Pressable Component Android iOS Example Tutorial.
Contents in this project React Native Pressable Component Android iOS Example Tutorial:
1. Import Text, View, StyleSheet and Pressable component in your project’s main App.js file.
1
2
3
|
import React, { Component } from ‘react’;
import { Text, View, StyleSheet, Pressable } from ‘react-native’;
|
2. Creating our main Export default App class extends Component. This is our main Root parent View class.
1
2
3
4
5
|
export default class App extends Component {
}
|
3. Creating a function named as printLog(). Inside this function we would simply call a Console Log message. We would call this function on button Click event.
1
2
3
4
5
|
printLog =()=>{
console.log(‘Pressable Clicked !!!’);
}
|
4. Creating render’s return block now here we would call our main Root Parent View.
1
2
3
4
5
6
7
8
9
|
render() {
return (
<View style={styles.MainContainer}>
</View>
);
}
|
5. Creating a Text component and 1 Pressable component in Root View.
- onPress :- Used to set click event on Component.
- pressed : Pressed contains a Boolean value which depends whether the Pressable component is clicked or Not. Using the pressed value we can easily change Style or Inside Text of Pressable component. So when user clicks on the button it will automatically change its layout and on releasing the button it will again change its layout.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<Text style={styles.text}> Pressable Component in React Native </Text>
<Pressable
onPress={this.printLog}
style={({ pressed }) => ({
backgroundColor: pressed ? ‘#FF3D00’ : ‘#33691E’,
borderRadius: 8,
})}>
{({ pressed }) => (
<Text style={styles.Pressable_text}>
{pressed ? ‘Pressed!’ : ‘Press Me’}
</Text>
)}
</Pressable>
|
6. Creating Style.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#FAFAFA’,
},
Pressable_text: {
fontSize: 24,
color:‘#ffff’,
textAlign: ‘center’,
padding: 20,
},
text: {
fontSize: 24,
color:‘#000’,
textAlign: ‘center’,
padding: 20
},
});
|
7. 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import React, { Component } from ‘react’;
import { Text, View, StyleSheet, Pressable } from ‘react-native’;
export default class App extends Component {
printLog =()=>{
console.log(‘Pressable Clicked !!!’);
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={styles.text}> Pressable Component in React Native </Text>
<Pressable
onPress={this.printLog}
style={({ pressed }) => ({
backgroundColor: pressed ? ‘#FF3D00’ : ‘#33691E’,
borderRadius: 8,
})}>
{({ pressed }) => (
<Text style={styles.Pressable_text}>
{pressed ? ‘Pressed!’ : ‘Press Me’}
</Text>
)}
</Pressable>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
backgroundColor: ‘#FAFAFA’,
},
Pressable_text: {
fontSize: 24,
color:‘#ffff’,
textAlign: ‘center’,
padding: 20,
},
text: {
fontSize: 24,
color:‘#000’,
textAlign: ‘center’,
padding: 20
},
});
|
Screenshots: