Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda