Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Cómo obtener de un componente de interruptor personalizado su valor de estado actual en mis formularios de gancho de reacción

Tengo un componente Switch personalizado que quiero usar para mis formularios en react-native. El dilema que tengo es cómo obtener de este componente de interruptor su valor de estado actual.

Tengo una versión del interruptor que funciona en este componente:

 /** @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> ); }

En mi pantalla de formulario tengo esto:

 <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 answers
Answer question

0

Debe elevar el estado isEnabled hasta su componente principal, ese es su formulario.

Esto podría verse de la siguiente manera.

 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> ); }

Entonces, en su forma

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!