1

I am implementing a semantic-ui-react dropdown and I am unable to set the value of this dropdown on change event.

Help would be appreciated

//options object
const options =  [
  {name: "val1" , label: "val1"},
  {name: "val2" , label: "val2"},
  {name: "val3" , label: "val3"}
]

this.state= {location: ''} //state object


//onchange event
dropdownChange = (event: React.SyntheticEvent<HTMLElement>, data:any) => {
    this.setState({[data.name]:data.value})
}

//select component
<Select options={options}
  name="location"  
  value={this.state.location}
  onChange={this.dropdownChange}
/>

2 Answers 2

2

EDIT: You need to add a value key to the array of data.

Look at how they generate data in their example :

const countryOptions = _.map(faker.definitions.address.country, country => ({
  key: country,
  text: country,
  value: country
}));

OLD ANSWER: There is no such propertie as data.name in the onChange handler (it prints undefined in this codesandbox)

I guess you should populate location key in the state :

dropdownChange = (event: React.SyntheticEvent<HTMLElement>, data:any) => {
    this.setState({location: data.value})
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using this as a general handler , this.setState({[data.name]:data.value}) ; Also I am able to get the info related to this data object, but "value" field is not getting set
I missed the obvious: you need to add a value key in your array of data :)
0

You can try like this.

//onchange event
dropdownChange = (event: React.SyntheticEvent<HTMLElement>, data:any) => {
    this.setState({[event.target.name]:data.value})
}

1 Comment

Able to get the name property, the problem is only with data.value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.