DEV Community

jan
jan

Posted on

How to get an alert message when changing the options in react?

I am having an ADD button which allows me to add some objects to the option that I selected in the dropdown. And I also have a function attached to that Add Button.

` onAddClicked = () =>{
this.setState({
showOptions: true
});

}`
Enter fullscreen mode Exit fullscreen mode

<button type="button" onClick={this.onAddClick}
style={{ float: "right", marginLeft: "10px", marginRight: "10px" }}
id="AddSelectedTag"
className={((this.state.selectedOptions ?this.state.selectedOptions.length === 0 : true) ?
"re-btn-primary-inactive" : "re-btn-primary-blue")}disabled={(this.state.selectedOptions ?
this.state.selectedOptions.length === 0 : true) ? true : false}>Add
</button>

I can select and option and few objects under it. Now I need to display an error message when I want to change my option after selecting few objects under that. I am having state for my selected options as "this.state.selectedOptions". I am trying this in my onAddClick().

if(this.state.selectedOptions ? this.state.selectedOptions.value : null)
{
alert("selectedOptions value has changed");
}

But this is returning an alert message everytime I click on the Add button, not when I try to change the state

Top comments (1)

Collapse
 
pranathitammina1 profile image
jan

It's taking me somewhere else