Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Media Upload Progress component converting from Class to Function #56256

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from

Conversation

dantovbein
Copy link

@dantovbein dantovbein commented Nov 17, 2023

Based on #22890

What?

Migrate the MediaUploadProgress react native component, currently as a React Class, to a Functional Component.

Why?

React introduced Hooks after 16.8.* version.
👇 Taken from their official documentation .

We recommend defining components as functions instead of classes. See how to migrate.

How?

Following this document

Testing Instructions

  1. Open a post or page.
  2. Insert an image block.
  3. Upload a new image

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Nov 17, 2023
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @dantovbein! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@dantovbein dantovbein marked this pull request as ready for review November 17, 2023 14:19
@dantovbein
Copy link
Author

An important thing to mentions is that I've used the combination of useCallback and useMemo hooks to prevent the pre-render.

@Mamaduka Mamaduka added the Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) label Nov 30, 2023
@SiobhyB SiobhyB added [Type] Code Quality Issues or PRs that relate to code quality Mobile App - Automation Label used to initiate Mobile App PR Automation labels Nov 30, 2023
Copy link
Contributor

@SiobhyB SiobhyB left a comment

Choose a reason for hiding this comment

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

@dantovbein, thank you for your contribution! It's cool to see moves to migrate class components to hooks. :)

I added a couple of comments related to the dependency arrays. I'm concerned about including all the props in the arrays, and think it's worth reviewing whether there are different approaches to take.

For the mediaUpload function, for example, perhaps we could manage addMediaUploadListener via a useEffect hook, then call mediaUpload as a regular function? (As I was looking through the code, I also wondered if it may be made more readable by separating that logic into its own useMediaUpload hook. But, I will leave that implementation decision to you.)

In addition, I noticed tests in the following suites are failing following the changes in this PR:

  • packages/block-editor/src/components/media-upload-progress/test/index.native.js
  • packages/block-library/src/audio/test/edit.native.js
  • packages/block-library/src/file/test/edit.native.js

Would you be able to take a look into these? I believe they're false positives, but the code will need to be updated to reflect the changes from this PR. We have steps for getting set up with the native tests here. If you have questions about setting up, please feel welcome to either reach out here or in the #mobile channel on Slack.

Thank you again for your contribution here! I'd be happy to take another look and do further testing if you can address those initial comments. 🙇‍♀️

}
},
[
props,
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm hesitant about passing all props to the dependency array. It feels like it defeats the purpose of having the array if the function's going to update on every render, and also doesn't seem necessary to me.

In the original code, mediaUpload is only called when the component is mounted. Could we maybe refactor this to use useEffect or potentially move some of this logic to a separate custom hook within this file? I think we could make this even cleaner.

progress,
progressBarStyle,
renderContent,
props,
Copy link
Contributor

Choose a reason for hiding this comment

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

Sharing similar concerns about passing all props to the dependency array, I'm worried it could lead to some unwanted side effects or unnecessary renders. Could we review the dependencies that are being used here to ensure they're all necessary?

@etoledom etoledom removed their request for review February 22, 2024 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Mobile App - Automation Label used to initiate Mobile App PR Automation Mobile App - i.e. Android or iOS Native mobile impl of the block editor. (Note: used in scripts, ping mobile folks to change) [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants