04-Apr-2023
.
Admin
Hi Guys,
In this blog, I will explain you how to create switch in react native. You can easily create switch in react native. First i will import namespace Switch
Here, I will give you full example for simply display switch using react native as bellow.
Step 1 - Create project
In the first step Run the following command for create project.
expo init switch
Step 2 - App.js
In this step, You will open App.js file and put the code.
import React, { useState } from "react";
import { View, Switch, StyleSheet } from "react-native";
export default function App() {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
return (
<View style={styles.container}>
<Switch
trackColor={{ false: "red", true: "green" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: "center",
justifyContent: "center"
}
});
Step 3 - Run project
In the last step run your project using bellow command.
npm start
Output
It will help you...
#React Native