- Create Components folder under src folder
- Add SwitchComponentEXP1.js file and add below Code
import React, { useState } from "react";
import { StyleSheet, Text, View, Switch } from "react-native";
const SwitchComponentEXP1 = () => {
const [SwitchOnValueHolder, setSwitchOnValueHolder] = useState(false);
const ShowAlert = value => {
setSwitchOnValueHolder(value);
if (value === true) {
alert("Switch is On.");
} else {
alert("Switch is Off.");
}
};
return (
<View style={styles.MainContainer}>
<Text> Switch Component example </Text>
<Text> Switch </Text>
<Switch
onValueChange={value => ShowAlert(value)}
style={{ marginBottom: 10 }}
value={SwitchOnValueHolder}
/>
</View>
);
};
export default SwitchComponentEXP1;
const styles = StyleSheet.create({
MainContainer: {
justifyContent: "center",
alignItems: "center"
}
});
- Open App.js file and update code as below
import React from 'react';
import { View,StyleSheet } from 'react-native';
import SwitchComponentEXP1 from './src/Component/SwitchComponentEXP1'
const App=()=> {
return (
<View style={styles.container}>
<SwitchComponentEXP1/>
</View>
);
}
export default App;
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
paddingTop:80
},
});
No comments:
Post a Comment