score:1
Accepted answer
I made it by setting value
as a index and then get the element by indexed value from the array
handleCountryChange(e){
this.setState({
country: countries[e.target.value].value,
currency: countries[e.target.value].currency
})
}
<select className="form-control input-lg" name="country" onChange={(e) => this.handleCountryChange(e)}>
{ countries.map((val, index) => {
return(<option value={index} key={index}>{val.name}</option>)
})}
</select>
score:1
Put the onChange
handler on the select
element instead.
Example
class App extends React.Component {
state = {
countries: [
{
name: "United States",
value: "US",
currency: "USD"
},
{
name: "Israle",
value: "IL",
currency: "ILS"
},
{
name: "United Kingdom",
value: "UK",
currency: "GBP"
}
]
};
handleCountryChange = event => {
const { value } = event.target;
const option = this.state.countries.find(
country => country.value === value
);
console.log(option);
};
render() {
const { countries } = this.state;
return (
<select
className="form-control input-lg"
name="country"
placeholder="Country"
onChange={this.handleCountryChange}
>
{countries.map((val, index) => {
return (
<option value={val.value} key={index}>
{val.name}
</option>
);
})}
</select>
);
}
}
score:2
You should put the handler on select
element. Not on individual option
.
Try the following..
<select className="form-control input-lg" name="country" placeholder="Country" onChange={(event) => this.handleCountryChange(event)}>
{ countries.map((val, index) => {
return(<option value={val.value} key={index}>{val.name}</option>)
})}
</select>
Source: stackoverflow.com
Related Query
- onClick not working on options field
- onclick function is not working in react native application
- onClick not working React js
- simulate for onClick not working in enzyme
- anchor tag (a tag) onclick event handler not working
- Select is not working onClick IconComponent(dropdown-arrow) in react material ui
- @react-google-maps/api <GoogleMap> onClick not working
- ReactJS & Google MDL Button onClick not working
- React onClick event handler not working using Next.js
- react-select onChange not working after change options
- OnClick Listeners not working after closing full-screen dialog using react-material-ui
- onClick not working on my React component
- onclick props not working with Material UI?
- React onClick not working in any of my browsers, but for colleagues it does
- React OnClick for item not working
- React - setState on input field not working
- React hook form - Register field not working
- Yup validation on Select field (dropdown) react-hook-form not working
- onClick not working inside the pop up opened via React portals
- GlobalHotKeys(react-hotkeys) not working when focused on the input field
- button onClick methods not working in react app
- useState not working outside of input field (function based Reactjs)
- Onclick handler not working on multiple image carousel
- onclick handler not working on component
- OnClick not working when rendering React button on a custom dialog
- React - onClick function not working inside a flexbox
- onClick inside mapped object JSX for React NextJS not working
- onClick handler to call Material UI dialog box, is not working properly
- onClick in reactjs not working
- onClick is not working on Popper component in Material-UI Tooltip
More Query from same tag
- React Router browserHistory push and anchors
- React hooks - wait until the state has been updated
- Running two countdown timers sequentially in React. The second one is skipped
- SyntaxError: Unexpected token import - reactjs
- React Native UI components: RCTBubblingEventBlock/RCTDirectEventBlock do not seem to work
- External Vendor Scripts( JS, Jquery) Not working after render in ReactJS
- Node express.js respond to post request
- I am rendring brightcove player from componentDidMount() and componentDidUpdate() , DOM is getting updated but video is not initialized . React+Redux
- How to clear the search parameter that caused the error?
- How can I resize, then render to a canvas with react-redux
- How can I mock the window.alert method in jest?
- reactjs state not updating via redux
- How can I return a react component using withStyles?
- How to focus a react-router Link
- Clearing Input after submit With react hooks
- How to handle api errors using aws-amplify?
- `The 'async' modifier can only be used in TypeScript files.ts(8009)` error when using async in react.js
- Certain parameters returns function error using reactjs
- How to format date in ReactJS
- Antd - is there anyway to change background color of the Card title?
- The axios delete functionality is only deleting last user from table, not the one I click on
- Typescript - No index signature with a parameter of type 'string'
- React js set array of data in date picker
- Handle Click Outside of Render()
- TypeScript: Typing array / tuple with values or empty
- React Bootstrap scroll down to content after accordion is opened
- How to clear a sequence of audio with timeouts in React
- Scroll events unintentionally changing Material UI slider values
- How restart a closed video track , stopped using userStream.getVideoTracks()[0].stop()
- How to upload image using ReactJS and save into local storage?