I'm using @material-ui/lab v4.0.0-alpha.36. It seems that onOpen callback of Autocomplete components is getting invoked twice when clicking into field.
Example of the component usage:
<Autocomplete
value={props.value}
options={props.options}
loading={props.loading}
multiple={props.multiple}
renderInput={params => (
<TextField
{...params}
fullWidth
name={props.name}
label={props.label}
onBlur={props.onBlur}
InputProps={{
...params.InputProps,
endAdornment: (
<>
{props.loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</>
),
}}
/>
)}
onOpen={() => console.log('on open')} <-- this is printed twice
onChange={handleChange}
getOptionLabel={option => option.label}
getOptionSelected={(option, value) => option.value === value}
/>
onOpen callback should be invoked only once.
Steps:
onOpen callback.onOpen invoked twice.| Tech | Version |
| ----------- | ------- |
| Material-UI | v4.0.0-alpha.36 |
| React | v16.12.0 |
| Browser | Chrome |
| TypeScript | - |
| etc. | |
@Mindtraveller Why is this an issue?
@Mindtraveller Why is this an issue?
Because Input was opened once whereas callback is invoked twice.
It's the same like invoking onChange or any other callback twice.
Do you think this is expected behaviour?
I have added the waiting for users upvotes tag. I'm closing the issue as we are not sure it's important. So please upvote this issue if you are. We will prioritize our effort based on the number of upvotes.
Hi! I still have same problem.
So I want to share one of solutions. You can control "open" state.
Also you can call this in events of Autocomplete component: onClose, onClickCapture, onOpen (actually work without this event in my code)
```const [isOpen, changeIsOpen] = useState(false);
const openAutocomplete = (newOpenState: boolean) => {
if (isOpen === false && newOpenState !== isOpen) {
onOpen && onOpen();
}
if (isOpen === true && newOpenState !== isOpen) {
onClose && onClose();
}
changeIsOpen(newOpenState);
};
What's the issue? Why does it matter how many time the callback is fired?
What's the issue? Why does it matter how many time the callback is fired?
I want to load data from server when i click Autocomplete component: https://material-ui.com/ru/components/autocomplete/#Asynchronous.tsx
Issue: double trigger of "onOpen" event. So, when i click Autocomplete element its trigger twice "fetch" to my server.
To be sure:
Component render once, but onOpen triggered twice, so it's not re-render mistake
Does anyone has a reproduction? I can't reproduce.
Does anyone has a reproduction? I can't reproduce.
I just want to share some more information.
If put alert('on open') into event handler, handler will invoke infinitely. Maybe its related with animation, I don't know.
Also my component wrapped by mobx.
Example:
<Autocomplete
value={props.value}
options={props.options}
loading={props.loading}
multiple={props.multiple}
renderInput={params => (
<TextField
{...params}
fullWidth
name={props.name}
label={props.label}
onBlur={props.onBlur}
InputProps={{
...params.InputProps,
endAdornment: (
<>
{props.loading ? <CircularProgress color="inherit" size={20} /> : null}
{params.InputProps.endAdornment}
</>
),
}}
/>
)}
onOpen={() => alert('on open')} <-- this is alert infinitely
onChange={handleChange}
getOptionLabel={option => option.label}
getOptionSelected={(option, value) => option.value === value}
/>
Please provide a minimal reproduction test case. This would help a lot 馃懛 .
A live example would be perfect. This codesandbox.io template _may_ be a good starting point. Thank you!