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

feat: Don't require defaultProps for function components #156

Merged
merged 1 commit into from
Sep 3, 2024

Conversation

bradenmacdonald
Copy link
Contributor

@bradenmacdonald bradenmacdonald commented Jul 12, 2024

I think react/require-default-props is an annoying rule in TSX files, as often it's perfectly fine to have undefined as the default value of a prop, and TypeScript already forces you to handle it accordingly in your component implementation.

But eslint currently wants us to add a default value for no reason. See example below, where eslint complains that three of the props lack defaults, even though 2 have a default and the third makes sense to have no default.

Screenshot 2024-07-12 at 12 16 01 PM

In this example, we don't need onToggleChildren to have a default value of an empty function, because TypeScript will make sure we call it as onToggleChildren?.(path); to handle the case where it's undefined. Or the author may choose to add a default, but in any case there's no errors nor incorrect code resulting from a missing default, so I don't believe the eslint rule is providing any value at all.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Jul 12, 2024
@openedx-webhooks
Copy link

openedx-webhooks commented Jul 12, 2024

Thanks for the pull request, @bradenmacdonald!

What's next?

Please work through the following steps to get your changes ready for engineering review:

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.

🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads

🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

🔘 Let us know that your PR is ready for review:

Who will review my changes?

This repository is currently unmaintained.

To get help with finding a technical reviewer, tag the community contributions project manager for this PR in a comment and let them know that your changes are ready for review:

  1. On the right-hand side of the PR, find the Contributions project, click the caret in the top right corner to expand it, and check the "Primary PM" field for the name of your PM.
  2. Find their GitHub handle here.

Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

.eslintrc.js Outdated
overrides: [
{
// Disable default-props for TypeScript files; it's fine to have undefined prop values if declared that way
files: ['**/*.tsx'],
Copy link
Member

Choose a reason for hiding this comment

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

Definitely in favor of disabling/modifying this rule, especially as we adopt more TypeScript stuff. Though, I do wonder if it's worth disabling the rule for both .jsx and .tsx, given defaultProps for functions are technically deprecated and are expected to be removed in React 19. Non-TypeScript files could also probably incrementally start migrating to ES6 default function parameters today as well.

If we were to disable the rule across all .jsx and .tsx files now, would this config be moved under the rules property instead of overrides?


[suggestion] Also, I wonder if it'd be worth considering whether to modify the rule versus disabling it altogether. For example, I imagine we might still want linting on missing defaultProps for any class components that might still exist (where ES6 default function params aren't applicable?). Similarly, there is a rule option (i.e., functions) for changing the expected strategy for defaulting props; by default, it is defaultProps but could be changed to defaultArguments instead (see docs). Changing the functions option to defaultArguments seems like it might be preferable versus disabling the rule altogether? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd love to just disable the rule altogether.

As for changing to defaultArguments, that's fine with me but I think it would cause way too many warnings/errors with existing code which is written using defaultProps (unless that's what you want, to force a big migration).

I'd still prefer to disable it altogether in TypeScript files, because I often write code like this (real example), which I think is perfectly fine:

const SearchUI: React.FC<{ courseId?: string, closeSearchModal?: () => void }> = (props) => {
  ...

and yet with defaultArguments on, eslint wants me to write it much more verbosely as:

const SearchUI: React.FC<{ courseId?: string, closeSearchModal?: () => void }> = ({
  courseId = undefined,
  closeSearchModal = undefined,
}) => {
  ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adamstankiewicz Would you like me to update this to just disable the rule altogether? (for JSX and TSX)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adamstankiewicz I updated this to not required defaultProps for function components in general, and added a comment explaining why it's deprecated.

I'd love to change the rule to also require defaultArguments but I'm not sure how to roll that out without requiring major changes of existing code, so I think this is the best incremental improvement.

Could you please take a look and approve if it's good with you?

Copy link
Member

Choose a reason for hiding this comment

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

@bradenmacdonald I will take a look later this evening!

@bradenmacdonald bradenmacdonald changed the title feat: Don't require default props in TSX files feat: Don't require defaultProps for function components Sep 3, 2024
@adamstankiewicz adamstankiewicz merged commit 78245b3 into openedx:master Sep 3, 2024
5 checks passed
@openedx-webhooks
Copy link

@bradenmacdonald 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@edx-semantic-release
Copy link

🎉 This PR is included in version 4.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U released
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants