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.
handleChange is a callback to the onChange of the Select component. It won't fire when you change the state and the value prop changes because its not a Select event, you're just changing the value it displays. At least that's how I understand it. Why not just call handleChange() in someEvent? There are a lot of issues that are bugs that need to be addressed on this library, do you mind closing this and if you still need help, asking instead on stack overflow?
I'm facing the similar issue, when I try to change the state of an element via SELECT's onChange method it does not work. What else can be used?
constructor(props) {
super(props)
this.state = {
names: [],
equipments: [],
isLoaded: false,
inputValue: 'Select Item'
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({
inputValue: e.target.value
})
console.log("inputValue=" + e.target.value);
}
value={this.state.inputValue}
onChange={this.props.handleChange}
options={options} />
@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.
@dehghani-mehdi thank you for clarifying the doubt. I can see the object generator now.
I have one query, can one fetch the value of the selected item outside handleChange method so that it can be used for the filtering purpose? Or if you know any guide for the same please share.
appreciate.
@ShivaniBali I think here is not right place to discuss that. but as you asked for help I'll give you short answer. I need to see your whole component code though, but generally I would store selected item in state, as you did, and use the state.selectedItem.value any where in the code. I didn't catch this: "outside handleChange method", but there is no way to get selected value without onChange method.
I tried that way but then it returns value undefined error. Here I'm trying to fetch the current selected option from dropdown and then on the basis of the value I retrieve ID and check whether that ID exists in my names array and if yes then I pass that ID to another API as a parameter for the conditional data pull.
constructor(props) {
super(props)
this.state = {
names: [],
equipments: [],
isLoaded: false,
inputValue: null
};
}
handleChange = selectedOption => {
this.setState({ inputValue: selectedOption });
console.log(`Option selected: ${selectedOption.value}`);
};
componentDidMount() {
const token = localStorage.getItem("token");
console.log("Inside Component Drop Down =" + token);
let selectedElement = this.state.selectedOption.value; --> TypeError: Cannot read property 'value' of undefined. (It picks null I guess)
names.filter((element) => element.name == selectedElement
return let storeid = element.div
) -- I'm pretty new in using filter, so please consider it as a prototype for now.
let url = `https://functional/api/web_page/get_store/?storeid={storeid}`;
fetch(url, {
method: "GET",
headers: { "Content-Type": "application/json", "Authorization": `Token ${token}` },
credentials: "same-origin"
})
.then((results1) => {
return results1.json();
}).then(data2 => {
this.setState({
isLoaded: true,
equipments: data2,
})
});
}
Thank you.
Hi @ShivaniBali,
I wanted to check in and see if you were still encountering this issue. We're triaging issues at the moment to sustain the project going forward, thanks!
@bladey I'm the author and I'm not __ShivaniBali__, you mentioned her, why you added awaiting-author-response tag?
Hi All,
Thank you for looking into my query.
I had changed the way I was operating: I used : e.target.value and
controlled my handler. It had worked.
Please perform the closures on this ticket if needed. Also, let me know if
you guys need to have a look on my code. It's been long so I need to look
and will share the code snippet with you all.
Best,
Shivani
On Tue, Jun 23, 2020 at 8:53 AM Mehdi Dehghani notifications@github.com
wrote:
@bladey https://github.com/bladey I'm the author and I'm not
ShivaniBali, you mentioned her, why you added awaiting-author-response
tag?—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/JedWatson/react-select/issues/3401#issuecomment-648125714,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AINEK4XJ73QDAXZBJM4QSWTRYCQV7ANCNFSM4GT6NALQ
.
Apologies @dehghani-mehdi, my mistake - the communications in this issue were mixed up.
I'll be closing out this issue. If there are any other questions this can be reopened or feel free to create a new discussion.
Most helpful comment
@ShivaniBali if you use markdown to format your code it would be helpful.
React Selectdoesn't give you JSeventin itsonChange_like standard htmlselectelement_, I mean you can not go fore.target.value, instead it gives you selected object. change yourhandleChangemethod like below and you are good to go:selectedItemhere is an object containsvalueandlabel.