I am trying to get the value of the select to set the value to the typeOp variable of the useState
If I choose the egress option , the console shows "entry" and if I then choose the entry option, the console shows "egress" . Why does this happen?
here my code:
const [typeOp, setTypeOp] = React.useState('ingreso');
const captureType = (e) => {
setTypeOp(e.target.value);
console.log(typeOp);
}
return (
<Form.Control as="select" onChange={captureType}>
<option value={typeOp}>Ingreso</option>
<option value="egreso">Egreso</option>
</Form.Control>
);
This problem is partly because
console.log()
it shows you the value of the statetypeOp
before it is set.If you would put the
console.log()
between thecaptureType
andreturn
you could see the status already updated.But even doing that there would still be a bug to fix.
The main problem is
value
what you are putting in the first<option>
.It shouldn't be
{typeOp}
because as it is it will take the value of the state that when it reaches the value of "Egress" it won't change anymore.To correct it you should change the one
value
of the first option and the position of the oneconsole.log()
like this: