2
How do I get the value of select
, gave a onChange
, but the value is not being returned as expected.
<select
className="form-control"
value={area}
onChange={e => setAddArea(e.target.value)}
>
<option disabled selected>
Selecione
</option>
{getArea.map((ar, i) => (
<option key={i} value={ar.area}>
{ar.area}
</option>
))}
</select>;
async function handleCreate(e, area) {
e.preventDefault();
const response = await api.post("/pergunta", {
area,
pergunta
});
console.log(response.data);
}
const [area, setAddArea] = useState("");
How do you know you are not returning the expected value? This code seems half incomplete...
– Felipe Avelar
I am using React Hooks, I used useState
– Pedro Henrique
Where is your call to handleCreate?
– Anderson Henrique
I put on the button to when the click it send the data that will be received by the <button type="Submit" classname="btn btn-Outline-Success" onClick={handleCreate}>Add</button>
– Pedro Henrique
The problem is exactly in the way you are using the
handleCreate
, since it is receiving as parameter area, it is writing asundefined
that area, no?– Felipe Avelar
If the
e.target.value
is not Undefined, so the problem is in your methodsetAddArea
.– Ericson Willians
@Felipeavelar what I don’t understand is that with the input <input classname="form-control" value={question} onChange={e => setAddPergunta(e.target.value)} placeholder="Enter Question" /> can be done the same way it sends the correct value, only select that does not work
– Pedro Henrique
@Pedrohenrique edits your question, putting how is this component of yours. I imagine the problem is in
handleCreate
and not taking the value of select...– Felipe Avelar
@Felipeavelar But handleCreate is only a function that I will call it when the user clicks on the button.
– Pedro Henrique
@Pedrohenrique you saw the online example I put Online Example ? and even has in the answer ?
– novic
@Pedrohenrique the local example is also running! take a look
– novic