Skip to content

fix(select): fix infinite loop when using array - #24763

Closed
adamduren wants to merge 1 commit into
ionic-team:mainfrom
adamduren:fix-24584-ion-select-bug
Closed

adamduren wants to merge 1 commit into
ionic-team:mainfrom
adamduren:fix-24584-ion-select-bug

Conversation

@adamduren

Copy link
Copy Markdown
Contributor

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

When using multiple and passing an array of objects ionChange is emitted multiple times. The array is eventually stringified and the component becomes unusable.

Issue Number: #24584

What is the new behavior?

The array is not stringified and the component behaves as expected.

Does this introduce a breaking change?

  • Yes
  • No

Other information

@adamduren
adamduren requested a review from a team February 10, 2022 01:08
@github-actions github-actions Bot added the package: react @ionic/react package label Feb 10, 2022
import { defineCustomElement as defineIonToolbar } from '@ionic/core/components/ion-toolbar.js';
import { defineCustomElement as defineIonVirtualScroll } from '@ionic/core/components/ion-virtual-scroll.js';

const excludeValueProp = (_: any, props: any) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is unfortunately autogenerated, so any changes here will be overwritten every time the core package is built.

Instead, we will need to exclude ion-select from being auto generated here: https://github.com/ionic-team/ionic-framework/blob/main/core/stencil.config.ts#L72

and then create a new file in packages/react/src/components/ called IonSelect.tsx that is basically:

import type { JSX } from '@ionic/core/components';
import { defineCustomElement  } from '@ionic/core/components/ion-select.js';

import { createReactComponent } from './react-component-lib';

const excludeValueProp = (_: any, props: any) => {
  const newProps = {
    ...props
  };
  delete newProps['value'];
  return newProps;
}

export const IonSelect = /*@__PURE__*/createReactComponent<JSX.IonSelect, HTMLIonSelectElement>('ion-select', undefined, excludeValueProp, defineCustomElement);

and finally export it from packages/react/src/components/index.ts.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sean-perkins before I make the changes, does this approach to fixing the issue seem to be correct?

I set some breakpoints to try and further validate my changes and I believe this is correct. By providing the custom mutator you prevent the value prop from being passed to React.createElement. If it is passed the value value will be reflected to the attribute and stringified. The value value will still be updated by the componentDidUpdate lifecycle hook.

The issue seems to be pretty severe / high priority because I believe the use case of IonSelect[multiple] as a controlled component is broken in Ionic v6 (react).

It's worth noting this change may not be needed / could be reverted if full support for Web Components lands in React 18 which seems like it may be a possibility as full support is currently on the React experimental branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since React reflects props to attributes does it make sense for the render function to filter out any prop with an object value to avoid this potential kind of issue elsewhere?

@adamduren

Copy link
Copy Markdown
Contributor Author

@sean-perkins anymore thoughts on my responses?

@liamdebeasi

Copy link
Copy Markdown
Contributor

Thanks for the PR and apologies for the delay. We appreciate all the hard work you put into creating this PR.

We have an update coming in Ionic 7.0 that changes how ionChange is fired from components. To summarize, ionChange will only fire when the value property is changed as a result of a user gesture. You can read more about this change here: #25532

#24584 is resolved by these changes. As a result, we are going to close this PR in favor of the changes coming in Ionic 7.0 We expect these changes to provide a more stable and consistent experience for Ionic developers.

I have followed up on the issue in #24584 (comment) and have provided a dev build with these Ionic 7.0 changes for testing. Let me know if you have any questions!

@adamduren
adamduren deleted the fix-24584-ion-select-bug branch December 6, 2023 07:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: react @ionic/react package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants