question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onChange doesn't trigger when state updated

See original GitHub issue

Hi,

Thanks for the react-select!

I googled and I found nothing, so I opened this issue and I don’t know if this is an issue or not.

In Select2 world, triggering onChange is simple as this:

$('select').val('NEW_VALUE').trigger('change');

I think react-select is the replacement to select2 for react developers.

Is there any way to do the same with react-select?

class MyCom extends Component {
    constructor(){
        this.items = [{ label: 'one', value: 1} , { label: 'two', value: 2} ];
        this.state = { 
            selected: { label: 'one', value: 1}			
        };
    }
	
    handleChange(selectedValue){
        console.log(selectedValue);
    }
	
    someEvent() {
        // selected item will be updated after following code but handleChange doesn't trigger.
        this.setState({selected: { label: 'two', value: 2}});
    }
	
    render(){
        return (
            <Select
                onChange={this.handleChange}
                value={this.state.selected}
                options={this.items}
            />
        );
    }
}

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
dehghani-mehdicommented, Nov 12, 2019

@ShivaniBali if you use markdown to format your code it would be helpful.

React Select doesn’t give you JS event in its onChange like standard html select element, I mean you can not go for e.target.value, instead it gives you selected object. change your handleChange method like below and you are good to go:

handleChange(selectedItem) {
    this.setState({
        inputValue: selectedItem
    });
    console.log("inputValue=" + selectedItem);
}

selectedItem here is an object contains value and label.

1reaction
bladeycommented, Jun 24, 2020

Apologies @dehghani-mehdi, my mistake - the communications in this issue were mixed up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why changing value in state doesn't trigger onChange of input ...
The value of the input is updated correctly as it should be but onChange is never called unless I updated value directly in...
Read more >
What Every React Developer Should Know About State
1. State updates with useState are not merged · 2. State hooks trigger a re-render, useRef does not · 3. State updates should...
Read more >
Simulate React On-Change On Controlled Components
Manually trigger onChange when component is updated programmatically ... The components are now controlled with React state update triggers.
Read more >
Don't useEffect as callback! - Johannes Kettmann
It doesn't cause an error, but it causes unnecessary re-renders and code ... State update triggering a callback ... onChange={onChange}.
Read more >
How do I make it so `onChange` doesn't trigger when ... - Reddit
onChange will fire every time a radio button is clicked, even if already selected. How should I be using onChange ? HTML <div...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found