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

330
Visualizações
React typescript onChange muestra un error cuando intento agregarlo al interruptor de interfaz de usuario de material

Estoy planeando hacer un show y ocultar usando el interruptor. Cuando enciendo el interruptor, debe mostrar el componente y cuando enciendo el interruptor, debe ocultar el componente.

Esta es la codificación que hice.

 export const VirtualEventSection = ({ control, onSearch, onSearchInputChange, searchInputValue, }: VirtualEventSectionProps) => { const { formatMessage } = useLocale(); const styles = useStyles(); const [state, setState] = useState(false); const handleSwitchChange = (event: React.ChangeEvent<HTMLInputElement>) => { setState(event.target.checked); }; return ( <Card className={styles.actionCard}> <Grid container spacing={1} alignItems="flex-start"> <Grid item sm={12} xs={12}> <SwitchWithLabel name="virtualEnabled" label={formatMessage({ id: 'form_event.event_toggle_lable' })} control={control} checked={state} onChange={handleSwitchChange} /> {state && state === true ? ( <LocationSelection control={control} onSearch={onSearch} onSearchInputChange={onSearchInputChange} searchInputValue={searchInputValue} /> ) : ( <h1></h1> )} </Grid> </Grid> </Card> ); };

Pero cuando agrego así, aparece un error en onChange . este es el error que tengo

 Type '(event: React.ChangeEvent<HTMLInputElement>) => void' is not assignable to type '({ name, checked }: OnChangeProps<"virtualEnabled">) => void'. Types of parameters 'event' and '__0' are incompatible.Type 'OnChangeProps<"virtualEnabled">' is missing the following properties from type 'ChangeEvent<HTMLInputElement>': target, nativeEvent, currentTarget, bubbles, and 11 more.

Este es el componente switchWithLable creado

 export const SwitchWithLabel = <T extends string = string>({ label, name, control, ...switchProps }: SwitchWithLabelProps<T>) => { const styles = useStyles(); return ( <Controller name={name} control={control} render={({ value, onChange }) => ( <FormControlLabel className={styles.label} label={label} control={ <Switch name={name} onChange={async ({ checked }) => { onChange(checked); }} checked={value} {...switchProps} /> } /> )} /> ); };

¿Alguien puede ayudarme a lograr el objetivo? Intenté varias veces y todavía muestra el error.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

No necesita tener un event: React.ChangeEvent<HTMLInputElement> . Puede cambiar el estado del interruptor con una función simple.

 const handleSwitchChange = () => { setState(!state); };

Código completo =>

 export const VirtualEventSection = ({ control, onSearch, onSearchInputChange, searchInputValue, }: VirtualEventSectionProps) => { const { formatMessage } = useLocale(); const styles = useStyles(); const [state, setState] = useState(false); const handleSwitchChange = () => { setState(!state); }; return ( <Card className={styles.actionCard}> <Grid container spacing={1} alignItems="flex-start"> <Grid item sm={12} xs={12}> <SwitchWithLabel name="virtualEnabled" label={formatMessage({ id: 'form_event.event_toggle_lable' })} control={control} checked={state} onChange={() => handleSwitchChange()} /> {state && state === true ? ( <LocationSelection control={control} onSearch={onSearch} onSearchInputChange={onSearchInputChange} searchInputValue={searchInputValue} /> ) : ( <h1></h1> )} </Grid> </Grid> </Card> ); };
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