onChange doesn't trigger when state updated
See original GitHub issueHi,
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:
- Created 5 years ago
- Comments:11 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ShivaniBali if you use markdown to format your code it would be helpful.
React Select
doesn’t give you JSevent
in itsonChange
like standard htmlselect
element, I mean you can not go fore.target.value
, instead it gives you selected object. change yourhandleChange
method like below and you are good to go:selectedItem
here is an object containsvalue
andlabel
.Apologies @dehghani-mehdi, my mistake - the communications in this issue were mixed up.