Problem with multiple select when onChange event we update a state
See original GitHub issueDescription
I have a problem with multiple select when onChange event we update a state. handleFilterChange is called but the dropdown is automatically closing. that I don’t want.
Steps to Reproduce
const [filterType, setFilterType] = useState<string[]>([]);
const handleFilterChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
let arr = [];
for (let i = 0; i < e.target.selectedOptions.length; i++) {
arr.push(e.target.selectedOptions[i].value);
}
setFilterType(arr);
}
<Select
label="filters"
onChange={e => handleFilterChange(e)}
id="filter"
icon={<Icon>filters</Icon>}
s={12}
m={6}
multiple
options={{
classes: 'browser-default',
dropdownOptions: {
alignment: 'left',
autoTrigger: true,
closeOnClick: false,
constrainWidth: true,
coverTrigger: true,
hover: false,
inDuration: 150,
outDuration: 250
}
}}
value={}
>
<option disabled value="">Choose your option</option>
<option value="1">Choose your option</option>
<option value="2">Choose your option</option>
</Select>
Expected behavior: [What you expect to happen]
the dropdown should not close.
Actual behavior: [What actually happens]
Versions
“react-materialize”: “^3.9.3”, “react”: “^16.10.2”, “materialize-css”: “^1.0.0-rc.2”,
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Select value doesnt change the first time I trigger onChange ...
The problem is that when you provide onChange prop to select component it become a controlled component. For more information: React Docs ...
Read more >SCR19: Using an onchange event on a select element ... - W3C
The objective of this technique is to demonstrate how to correctly use an onchange event with a select element to update other elements...
Read more >Integrating with Other Libraries - React
We subscribe to the change event (and unsubscribe on unmounting), and when it happens, we update the state with the model's current attributes....
Read more >How can I handle onchange event for multiple select elements ...
Coding example for the question How can I handle onchange event for multiple select elements from map items-Reactjs.
Read more >How to change a select's options based on another dropdown ...
Select in HTML allows us to choose among multiple values using the dropdown. Here, we are talking about changing select options based on...
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 Free
Top 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

As you said this is an issue with
materializecss. When calling set state, the component is rerendered and the select is created again, therefor returning to its original closed state. If Select had aopenprop that we could then pass to materializejs this could be solved… but it doesn’t.A workaround could be to only set state when the select is closed… maybe
I had to switch for something else until the issue is fixed… multiselect-react-dropdown does the job pretty well and has a better integration in React than
react-materialize.