Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
How to obtain from a custom switch component its current state value on my react-hook-forms

I have a custom Switch component that I want to use for my forms in react-native. The dilemma I have is how to obtain from this switch component its current state value.

I got a version of the switch working on this component:

/** @format */

// BASE
import React, { useState } from 'react';
import {
  View,
  Platform,
  TouchableNativeFeedback,
  TouchableOpacity,
  Switch,
} from 'react-native';

// CONSTANTS
import Styles from '../../../../constants/Styles';

// TYPE
import SubtitleOne from '../../../type/SubtitleOne';

// COMPONENTS
import ListItem from '../ListItem/ListItem';

export default function SingleLineWithSwitch(props) {
  const [isEnabled, setIsEnabled] = useState(props.value);
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);

  return (
    <ListItem onPress={() => toggleSwitch()}>
      <View style={Styles.listSingleCointainer}>
        <SubtitleOne style={Styles.listTitle}>{props.title}</SubtitleOne>
        <View style={Styles.listSwitchView}>
          <Switch
            thumbColor={props.thumbColor}
            trackColor={props.trackColor}
            style={Styles.switchControl}
            value={isEnabled}
            onValueChange={toggleSwitch}
          />
        </View>
      </View>
    </ListItem>
  );
}

On my form Screen I have this:

<Controller
        control={control}
        render={({ field: { onChange, onBlur, value } }) => (
          <SingleLineWithSwitch
            title='Visible'
            onBlur={onBlur}
            onChangeText={onChange}
            value={value}
          />
        )}
        name='visible'
      />
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to lift the state isEnabled up to you parent component, that is your form.

This could look as follows.

export default function SingleLineWithSwitch(props) {
  const isEnabled = props.enabled;
  const setIsEnabled = props.setIsEnabled;
  const toggleSwitch = () => setIsEnabled(previousState => !previousState);

  return (
    <ListItem onPress={() => toggleSwitch()}>
      <View style={Styles.listSingleCointainer}>
        <SubtitleOne style={Styles.listTitle}>{props.title}</SubtitleOne>
        <View style={Styles.listSwitchView}>
          <Switch
            thumbColor={props.thumbColor}
            trackColor={props.trackColor}
            style={Styles.switchControl}
            value={isEnabled}
            onValueChange={toggleSwitch}
          />
        </View>
      </View>
    </ListItem>
  );
}

Then, in your form

const [enabled, setIsEnabled] = useState(value)
<Controller
        control={control}
        render={({ field: { onChange, onBlur, value } }) => (
          <SingleLineWithSwitch
            title='Visible'
            onBlur={onBlur}
            onChangeText={onChange}
            enabled={enabled}
            setIsEnabled={setIsEnabled}
          />
        )}
        name='visible'
  />
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda