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

SFC detection: React 16 #1510

Closed
2 tasks
ljharb opened this issue Nov 1, 2017 · 8 comments
Closed
2 tasks

SFC detection: React 16 #1510

ljharb opened this issue Nov 1, 2017 · 8 comments

Comments

@ljharb
Copy link
Member

ljharb commented Nov 1, 2017

Prior to React 16, separate from what detection rules are actually in the code, an SFC conceptually meets the following static criteria:

  • a function
  • name starts with a capital letter if not stored on an object property
  • returns JSX, or returns null
  • takes no more than two arguments (props and context, destructured or otherwise)
  • has any of the following static properties: propTypes, defaultProps, contextTypes, displayName
  • if it's used anywhere in the current file in a createElement/jsx context

In React 16, components can also return strings, and/or arrays.

This issue is to track:

  • updating our SFC detection to meet the above pre-16 criteria, if needed
  • updating our SFC detection, when the React version pragma is "16", to handle SFCs returning fragments.
@petersendidit
Copy link
Contributor

While looking at how to implement this here is some thoughts I had.

Right now getParentStatelessComponent only verifies a few of the items above. It doesn't verify the name of the component starts with a capital letter nor does it check if it returns JSX or the number of arguments.... It's not as simple as updating getParentStatelessComponent to handle those cases because we have rules that are depending on getParentStatelessComponent finding "components" that don't match all the rules.

Example being the display name rule wanting to find this as a component and making it invalid because it doesn't have a display name.

import React, { createElement } from "react";
export default (props) => {
  return createElement("div", {}, "hello");
};

Right now the Components util stores its confidence of the node being a component (0=banned, 1=maybe, 2=yes) but the list method on Components doesn't have a way to ask for "maybe" components.

We might have to define what of those above rules are required to be a "maybe" component, update list to be able to return both "maybe" and "yes" components on request and then update the rules to ask for the both when needed.

@ardok

This comment has been minimized.

@ljharb

This comment has been minimized.

@ardok

This comment has been minimized.

@ljharb

This comment has been minimized.

@Maxim-Mazurok

This comment has been minimized.

@ljharb

This comment has been minimized.

@ljharb
Copy link
Member Author

ljharb commented Sep 30, 2022

I think this is largely resolved; please open new issues if there's any further problems with SFC component detection.

@ljharb ljharb closed this as completed Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants