2
Yes, rookie in React/Typescript.
Before anyone tells me to go study the material, I would like to say that I looked at different approaches to this, with different points of view, but none clarified to me the doubt that is simple, let alone left a functional code.
I would simply capture the change of state in a checkbox component, that is, if your new state is checked(checked) or unchecked(unchecked).
I tried the 4 ways below, in none of them I get success:
onChange = { (e) => {setStatusAguardando(e.target.checked)} }
onChange = { (e) => {setStatusAguardando(e.target.checked)} }
onChange = { handleCBAguardando }
onChecked = { handleCBAguardando }
Including creating a specific handleCBA function to try to capture:
function handleCBAguardando(e: React.ChangeEvent<HTMLInputElement>) {
setStatusAguardando(e.target.checked);
}
What I got in the treatments?
- First attempt: Property 'value' does not exist on type 'Eventtarget'
- Second attempt: Property 'checked' does not exist on type 'Eventtarget'
- Third attempt: As below (unless mistaken, the indicated to
final to use "Event.target.checked" was just what I did on
previous):
Type '(e: Changeevent) => void' is not Assignable to type '(Event: Changeevent<{}>, checked: Boolean) => void'. Types of Parameters 'e' and 'Event' are incompatible. Type 'Changeevent<{}>' is not Assignable to type 'Changeevent'. Type '{}' is Missing the following properties from type 'Htmlinputelement': Accept, align, alt, autocomplete, and 287 more.ts(2322) Formcontrollabel.d.ts(41, 3): The expected type comes from Property 'onChange' which is declared here on type 'Intrinsicattributes & Formcontrollabelprops' (JSX attribute) Formcontrollabelprops.onChange?: ((Event: React.Changeevent<{}>, checked: Boolean) => void) | Undefined Callback Fired when the state is changed. @param Event The Event source of the callback. You can pull out the new state checked by accessing Event.target.checked (Boolean). - Fourth attempt: It was another attempt without knowing what else to do: Type '{ id: string; name: string; control: Element; label: string; labelPlacement: "end"; checked: Boolean; onChecked: (and: Changeevent) => void; }' is not Assignable to type 'Intrinsicattributes & Formcontrollabelprops'. Property 'onChecked' does not exist on type 'Intrinsicattributes & Formcontrollabelprops'. Did you Mean 'checked'? ts(2322)
My doubt: What is the correct way to capture the change of the state in a checkbox component, i.e., get your new state marked(checked) or unchecked(unchecked). After all, what is the right way to do this? Could someone explain in a simple and direct way to the point?
Observing: I am using typescript https://material-ui.com/ but I believe there is no conflict about that.
All help is welcome!
The following is the complete code summarized for better understanding of the whole.
import React, { FormEvent, useState } from "react";
import Checkbox from '@material-ui/core/Checkbox';
import FormGroup from '@material-ui/core/FormGroup';
import FormControlLabel from '@material-ui/core/FormControlLabel';
import FormControl from '@material-ui/core/FormControl';
import FormLabel from '@material-ui/core/FormLabel';
...
function ListaAgendamentos() {
const [statusAguardando, setStatusAguardando] = useState(true);
const [statusConfirmado, setStatusConfirmado] = useState(false);
...
function handleCBAguardando(e: React.ChangeEvent<HTMLInputElement>) {
setStatusAguardando(e.target.checked);
}
...
return (
<div>
<form id="buscar-lista-agendamentos" onSubmit={ buscarListaAgendamentos }>
<Cabecalho />
...
<div>
<FormControl component="fieldset">
<FormLabel component="legend" >Status Desejado</FormLabel>
<FormGroup aria-label="position" row>
<FormControlLabel
id="status_Aguardando"
name="status_Aguardando"
control={<Checkbox color="primary" />}
label="Aguardando"
labelPlacement="end"
checked={statusAguardando}
// AQUI CREIO ESTAR O PROBLEMA, NÃO CONSIGO CAPTURAR A MUDANÇA EM NENHUMA DAS FORMAS ABAIXO PARA ATRIBUIR AO MEU setStatusAguardando()
// onChange = { (e) => {setStatusAguardando(e.target.checked)} }
onChange = { (e) => {setStatusAguardando(e.target.checked)} }
// onChange = { handleCBAguardando }
// onChecked = { handleCBAguardando }
/>
<div>
...
</form>
</div>
);
}
export default ListaAgendamentos;
Thank you community!
What is your doubt?
– novic
Hello novic, thank you for the return: So my question is put in the title of the question: How do I get a simple status change from a chekbox in React? In the 4 different attempts presented the pointed errors. What’s the right way to do it? (I added a highlight to the issue in the text and if you can clarify was very grateful!)
– Marcs