El componente Switch en React Native se utiliza para crear interruptores de activación y desactivación (tipo on/off).
Es útil para casos donde el usuario necesita habilitar o deshabilitar una función.
import React, { useState } from 'react';
import { View, Text, Switch, StyleSheet } from 'react-native';
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
return (
<View style={styles.container}>
<Text style={styles.text}>¿Está activado?</Text>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 20,
marginBottom: 10,
},
});
export default App; value: Controla si el interruptor está activado o desactivado (booleano).onValueChange: Función que se ejecuta cuando el interruptor cambia de estado.trackColor: Colores del track (el fondo del interruptor) para los estados false y true.thumbColor: Color del círculo del interruptor.ios_backgroundColor: Color de fondo del interruptor en iOS (solo iOS).disabled: Booleano que determina si el interruptor está deshabilitado o no.import React, { useState } from 'react';
import { View, Switch, Text, StyleSheet } from 'react-native';
const SwitchExample = () => {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(prev => !prev);
return (
<View style={styles.container}>
<Text style={styles.icon}>{isEnabled ? '💡' : '🔅'}</Text>
<Text style={styles.text}>
{isEnabled ? 'Luz Encendida' : 'Luz Apagada'}
</Text>
<Switch
trackColor={{ false: '#767577', true: '#81b0ff' }}
thumbColor={isEnabled ? '#f5dd4b' : '#f4f3f4'}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#fff',
},
icon: {
fontSize: 64,
marginBottom: 10,
},
text: {
fontSize: 20,
marginBottom: 20,
fontWeight: '600',
},
});
export default SwitchExample; Puedes personalizar el Switch cambiando los colores del track y el círculo, así como el color de fondo en iOS.
import React, { useState } from 'react';
import { View, Switch, Text, StyleSheet } from 'react-native';
const App = () => {
const [isEnabled, setIsEnabled] = useState(false);
const toggleSwitch = () => setIsEnabled(previousState => !previousState);
return (
<View style={styles.container}>
<Text style={styles.text}>¿Activado? {isEnabled ? "Sí" : "No"}</Text>
<Switch
trackColor={{ false: "#767577", true: "#81b0ff" }}
thumbColor={isEnabled ? "#f5dd4b" : "#f4f3f4"}
ios_backgroundColor="#3e3e3e"
onValueChange={toggleSwitch}
value={isEnabled}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
text: {
fontSize: 18,
marginBottom: 10,
},
});
export default App; El componente Switch en React Native es ideal para controlar el estado de un interruptor (encendido o apagado) y lo hace muy altamente personalizable, permitiendo cambiar colores y estilos para adaptarse a la estética de tu aplicación.
Puedes usarlo para crear una experiencia de usuario intuitiva y atractiva.