How to take the value of select

Asked

Viewed 2,901 times

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...

  • I am using React Hooks, I used useState

  • Where is your call to handleCreate?

  • 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>

  • The problem is exactly in the way you are using the handleCreate, since it is receiving as parameter area, it is writing as undefined that area, no?

  • If the e.target.value is not Undefined, so the problem is in your method setAddArea.

  • @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

  • @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...

  • @Felipeavelar But handleCreate is only a function that I will call it when the user clicks on the button.

  • @Pedrohenrique you saw the online example I put Online Example ? and even has in the answer ?

  • @Pedrohenrique the local example is also running! take a look

Show 6 more comments

2 answers

2

I guess I was just about to solve, when you create a variable area and its method of modification setAddArea (React Hooks) you have already done where the value of the selection will be, and as changes the select at the event onChange makes the change (e.target.value) and after this change in the variable area has the value of select as needed. If in the code you need to use the selection value, just assign the variable value area.

An example running in Codesandbox

function App () {
  const [selectValue, setSelectValue] = React.useState(1);  
  const list = [
    {id: 1, name: 'select 1'},
    {id: 2, name: 'select 2'},
    {id: 3, name: 'select 3'},
    {id: 4, name: 'select 4'},
  ];
  return (    
    <div>
      <p>{selectValue}</p>
      <select value={selectValue} onChange={e => setSelectValue(e.target.value)}>
        {list.map((item, index) => (
          <option value={item.id}>{item.name}</option>
        ))}        
      </select>
    </div>
  )
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>

You have a method in your code async function handleCreate(e, area) that you have the area as a parameter, but it is unnecessary to only use, example:

async function handleCreate(e) {
  e.preventDefault()        
  const response = await api.post('/pergunta', {
      area,//<-- isso já é o suficiente
      pergunta
  }) 
}
  • Why did I receive a negative vote? can you explain

  • As far as I can tell, that’s exactly what he’s doing and it doesn’t solve his problem...

  • @Lucky as you know it’s not that?

  • Because his example is practically the same as the question, only he is using an Handle, which is not contemplated in his example...

  • Osh, I did not vote negative, I will try to execute, but apparently, it is the same way I did, only returns Undefined, but thank you.

  • @Felipeavelar more look at his question and what he’s asking, I don’t deserve to take negative vote!

  • @Virgilionovic, I understand that you received as takes the value in onChange, but the doubt is related to the fact that it does not have the expected value in the handleCreate, not of not getting the value in select, because it picks up the same way you put in the example...

  • @Lucky, but I deserve a negative vote on the question: How to get the value of select React and look how I explained!

  • @Virgilionovic, I’ll remove the negative, but you agree that it doesn’t solve his problem?

  • @Felipeavelar solves yes, his problem is what is in the question not what we imagine, you need to understand that we answer according to the question not imagination, I explained how rescues the value of Select!

  • @Virgilionovic, now yes, with this edition solves the problem. (:

  • 2

    @Virgilionovic very much Thank you

Show 7 more comments

1


The problem is not in collecting the value of select, but in the fact that you are creating a new scope for the variable area, as I point out @Virgilionovic. Here is an example of an applicable component that solves your problem:

import React, { useState } from 'react';

function Example() {
  const [selectValue, setSelectValue] = useState(1);  
  const list = [
    {id: 1, name: 'select 1'},
    {id: 2, name: 'select 2'},
    {id: 3, name: 'select 3'},
    {id: 4, name: 'select 4'},
  ];

  function handleCreate(e) {
    e.preventDefault()
    alert(selectValue)  
  }

  return (    
    <div>
      <p>{selectValue}</p>
      <select value={selectValue} onChange={e => setSelectValue(e.target.value)}>
        {list.map((item, index) => (
          <option value={item.id}>{item.name}</option>
        ))}        
      </select>
      <button onClick={handleCreate}>
        Click me
      </button>
    </div>
  )
}

export default Example;

Note that in the handleCreate the variable has been removed area of the parameters it receives, precisely to not create a local variable for the function and take advantage of the area that is associated with the state of the component.

  • 1

    Thank you very much, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.