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: change defaultProps generation for react #1661

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

nmerget
Copy link
Contributor

@nmerget nmerget commented Jan 15, 2025

Description

Please provide the following information:

closes #1471

Make sure to follow the PR preparation steps in CONTRIBUTING.md before submitting your PR:

  • format the codebase: from the root, run yarn fmt:prettier.
  • update all snapshots (in core & CLI): from the root, run yarn test:update
  • add Changeset entry: from the root, run yarn g:changeset and follow the CLI instructions. Alternatively, use the Changeset Github Bot to create the file.

@nmerget nmerget requested a review from samijaber as a code owner January 15, 2025 10:01
Copy link

changeset-bot bot commented Jan 15, 2025

🦋 Changeset detected

Latest commit: d1bd12e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@builder.io/mitosis Patch
@builder.io/mitosis-cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

nx-cloud bot commented Jan 15, 2025

View your CI Pipeline Execution ↗ for commit d1bd12e.

Command Status Duration Result
nx run-many --target test ❌ Failed 4m 45s View ↗
nx run-many -t test:update ✅ Succeeded 4m 49s View ↗
nx e2e @builder.io/e2e-app ✅ Succeeded 1m 2s View ↗
nx run-many --target build --exclude @builder.i... ✅ Succeeded 3m 55s View ↗
nx build @builder.io/mitosis-site ✅ Succeeded 2m 25s View ↗

☁️ Nx Cloud last updated this comment at 2025-02-25 11:34:35 UTC

Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

@nmerget Thanks a lot for tackling this long overdue refactor!

There is however one big drawback to your approach: by writing props = { /* DEFAULT_PROPS */}, you are overwriting any props that do not have defaults!

See this example:

CleanShot 2025-01-15 at 08 03 11@2x

type Props = {
  foo?: string;
  bar?: string;
};

// BEFORE
const A = (props: Props) => <div>B: {JSON.stringify(props)}</div>;
A.defaultProps = { foo: 'bar' };

// AFTER
const B = (props: Props = { foo: 'bar' }) => (
  <div>A: {JSON.stringify(props)}</div>
);

// FIX
const C = (props: Props) => {
  props = { ...props, foo: 'bar' };
  return <div>C: {JSON.stringify(props)}</div>;
};

export default function App() {
  return (
    <>
      <A bar="another-prop" />
      <B bar="another-prop" />
      <C bar="another-prop" />
    </>
  );
}

I think we need to instead use the approach i provide in C

@nmerget
Copy link
Contributor Author

nmerget commented Jan 16, 2025

const C = (props: Props) => {
  props = { ...props, foo: 'bar' };
  return <div>C: {JSON.stringify(props)}</div>;
};

This would always overwrite the given props with the default value, wouldn't it? :P

@nmerget nmerget requested a review from samijaber January 17, 2025 11:40
@samijaber
Copy link
Contributor

const C = (props: Props) => {
  props = { ...props, foo: 'bar' };
  return <div>C: {JSON.stringify(props)}</div>;
};

This would always overwrite the given props with the default value, wouldn't it? :P

Woops, you're right. I should have written:

const C = (props: Props) => {
  props = { foo: 'bar', ...props };
  return <div>C: {JSON.stringify(props)}</div>;
};

To re-illustrate my point, here's an updated example:

CleanShot 2025-01-17 at 11 28 33@2x

As you can see, <B /> never sets the default-value in any situation.

Essentially, const MyComp = (props = DEFAULT_VALUES) => {} will never do anything because the props object will always exist. DEFAULT_VALUES will only be used if props is undefined, which is never the case (at worst it's an empty object).

@nmerget
Copy link
Contributor Author

nmerget commented Jan 20, 2025

Yes, I changed it to follow your suggestions. I also had to change other targets because the e2e tests were failing. ^.^

Copy link
Contributor

@samijaber samijaber left a comment

Choose a reason for hiding this comment

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

Requesting changes because one of your refactors caused a regression in the react generator

Comment on lines 142 to 144
if (type === 'getter') return getFunctionString(code.replace('get ', ''));
if (type === 'function')
return code.startsWith('async') ? code : getFunctionString(code);
Copy link
Contributor

Choose a reason for hiding this comment

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

this refactor seems to have introduced the regression: getFunctionString(code) doesn't do the same thing as ${key} = function ${code} in the original code

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'll look at it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Quick fix was to have an exception for rsc ^.^

…ault-props

# Conflicts:
#	packages/core/src/__tests__/__snapshots__/react.test.ts.snap
#	packages/core/src/__tests__/__snapshots__/stencil.test.ts.snap
#	packages/core/src/generators/stencil/component.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

React's defaultProps will be remove from function components in a future release
2 participants