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

Add react/no-object-type-as-default-prop to react/recommended #3863

Open
SimonSimCity opened this issue Dec 6, 2024 · 2 comments
Open

Add react/no-object-type-as-default-prop to react/recommended #3863

SimonSimCity opened this issue Dec 6, 2024 · 2 comments
Labels
semver-major Breaking change.

Comments

@SimonSimCity
Copy link

I propose to add the rule react/no-object-type-as-default-prop at least with warning if not error to the react/recommended set.

Reason: I don't see a good reason not to enable it. It helps you not to fall into the trap of endless rendering. I even had a case where I wrote a component which just seemed to fail in my test environment, but just because this was the only place I didn't set the prop which had an object as default.

If you see a reason not to enable this, I'm thrilled to hear!

@SimonSimCity
Copy link
Author

Here's just one way of creating a component which will have an endless render:

export const MyObject = ({
  myFancyThing = { test: 1 }, // Never set an object here as default! It will be a different object every time this element is rendered!
}: {
  myFancyThing?: { test: number };
}) => {
  const [foo, setFoo] = useState<string | undefined>(undefined);

  useEffect(() => {
    setFoo("")
  }, [myFancyThing]); // This will always run, because the dependency is always a different object!

  return (
    <...>
  );
};

@ljharb
Copy link
Member

ljharb commented Dec 6, 2024

Adding anything to the recommended config is a breaking change, but I agree it should be done at some point.

@ljharb ljharb added the semver-major Breaking change. label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
semver-major Breaking change.
Development

No branches or pull requests

2 participants