Switch component is used to perform ON-OFF type functionality in both android and iOS applications. The Switch component renders Boolean value and returns output in True & False form. When application user turn on the Switch button, it will return result in True from and on turn off event, it will eventually return False. So here is the complete step by step tutorial for React Native Simple Custom Switch Component.
Contents in this project React Native Simple Custom Switch Component example :
1. Import AppRegistry, StyleSheet, Text, View, Switch and Alert component in your project.
1
2
3
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Switch, Alert} from ‘react-native’;
|
2. Create constructor() in your project’s class and Inside that declare a State named as SwitchOnValueHolder and set its value as false . Here SwitchOnValueHolder is used to set the default state of Switch button, Means SwitchOnValueHolder value false sets the Switch default On Active state. If you set its value as True then it will by default shows in Turn Off state.
1
2
3
4
5
6
7
8
9
10
|
constructor(){
super();
this.state ={
SwitchOnValueHolder : false
}
}
|
3. Create a function named as ShowAlert with value parameter .Here value comes in True or False on Switch button ON – OFF event. If Switch button Turn on then it will receive True, If Switch button Turn Off then it will receive False.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
ShowAlert = (value) =>{
this.setState({
SwitchOnValueHolder: value
})
if(value == true)
{
//Perform any task here which you want to execute on Switch ON event.
Alert.alert(“Switch is On.”);
}
else{
//Perform any task here which you want to execute on Switch OFF event.
Alert.alert(“Switch is Off.”);
}
}
|
4. Create A view as root view and inside that root view create Switch button.
onValueChange : Trigger the Switch default Turn on off event and return result in Boolean(TRUE – FALSE) form.
style : Styling the Switch button.
value : Renders the default state value to show default switch button state.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
render() {
return (
<View style={styles.MainContainer}>
<Text style={{fontSize: 18}}> Switch </Text>
<Switch
onValueChange={(value) => this.ShowAlert(value)}
style={{marginBottom: 10}}
value={this.state.SwitchOnValueHolder} />
</View>
);
}
|
5. Create CSS class.
1
2
3
4
5
6
7
8
9
10
11
12
|
const styles = StyleSheet.create({
MainContainer :{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
margin: 10
}
});
|
6. Complete source code for index.android.js / index.ios.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
65
66
67
68
69
70
|
import React, { Component } from ‘react’;
import { AppRegistry, StyleSheet, Text, View, Switch, Alert} from ‘react-native’;
class Project extends Component {
constructor(){
super();
this.state ={
SwitchOnValueHolder : false
}
}
ShowAlert = (value) =>{
this.setState({
SwitchOnValueHolder: value
})
if(value == true)
{
//Perform any task here which you want to execute on Switch ON event.
Alert.alert(“Switch is On.”);
}
else{
//Perform any task here which you want to execute on Switch OFF event.
Alert.alert(“Switch is Off.”);
}
}
render() {
return (
<View style={styles.MainContainer}>
<Text style={{fontSize: 18}}> Switch </Text>
<Switch
onValueChange={(value) => this.ShowAlert(value)}
style={{marginBottom: 10}}
value={this.state.SwitchOnValueHolder} />
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: ‘center’,
alignItems: ‘center’,
flex:1,
margin: 10
}
});
AppRegistry.registerComponent(‘Project’, () => Project);
|
Screenshots in iOS mobile device application :
Screenshot in Android mobile phone application: