I use a select tag. I would like to be able to read the values from this select tag when I click a button. I have the following problem, if I don't read another value, but leave the first value Argentina, then my output is undefinded. What is the best option to get the value in a variable besides onChange. After all, that only works if the user has selected something.
import React, { useState } from "react";
function Finder() {
const [selectedValue, setSelectedValue] = useState();
const find= () => {
console.log(selectedValue);
};
return (
<div>
<h1 className="title is-3">Select your option</h1>
<div className="field has-addons">
<div className="control is-expanded">
<div className="select is-fullwidth">
<select
name="country"
onChange={(e) => setSelectedValue(e.target.value)}
>
<option value="Argentina">Argentina</option>
<option value="Bolivia">Bolivia</option>
<option value="Brazil">Brazil</option>
<option value="Chile">Chile</option>
<option value="Colombia">Colombia</option>
<option value="Ecuador">Ecuador</option>
<option value="Guyana">Guyana</option>
<option value="Paraguay">Paraguay</option>
<option value="Peru">Peru</option>
<option value="Suriname">Suriname</option>
<option value="Uruguay">Uruguay</option>
<option value="Venezuela">Venezuela</option>
</select>
</div>
</div>
<div className="control">
<button
type="button"
className="button is-primary"
onClick={() => find()}
>
Choose
</button>
</div>
</div>
</div>
);
}
export default Finder;