-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
new rule: forbid exporting React Components and non-components from the same module #3176
Comments
That is a very absurd restriction imo. The typical structure I see and use is that a file default-exports a React component, and named-exports its propTypes, and indeed in TS code, types are named-exported along with that. Is there any part of the ecosystem besides react-fast-refresh with this restriction? |
That kind of structure would be legit under this rule: when you look at the compiled js for a typescript file, any kind of type export is going to be stripped out, which would include your propsType. The rule is more to catch cases like this: export const myConstant = 100; or, maybe more insidiously export const myGlobalMutableState = new StateObject(); where we can't rely on the assumption that state is being managed by react. re: ecosystem question, fast-refresh is the current approach to hot module reload in react. Any bundler for react native or react-dom that has HMR is using fast-refresh under the hood. Most of the time though, it's not a problem even if you break the rule: it just means that the bundler and ultimately the browser are going to need to make and load bigger chunks up until the point where you hit one of these edge cases that causes things to break down. |
No, that would not include my non-TS propTypes object, which remains at runtime intentionally. |
oh, that's referring to the |
That seems like a huge flaw in current implementations of HMR, then. It seems like a poor idea to codify a hopefully temporary flaw in an eslint rule, since those tend to be cargo-culted into permanent best practice. |
Some quick notes here:
@ljharb, understand that you personally don't want to work on this... I imagine it's frustrating having to enforce a rule that you see as a "temporary flaw". I'm not here to defend this design decision... I don't even have context on why it was made. But as a React developer, I would love to see an ESLint rule that could help our team avoid breaking HMR. If this needs to be a third-party package I think that's fine. Commenting here to share context, and potentially solicit community interest from others on working on this. |
Lots of people still use PropTypes - the prop-types lib gets 18m downloads a week, so it should always be a subject of conversation anywhere, objectively. |
Hey folks, I was looking for a solution and found this rule: https://github.com/ArnaudBarre/eslint-plugin-react-refresh (@Ashoat take a look, it might help you :) Please notice that this rule was created for Vite, but I think it should work still (or you can use it as a starting point for your own implementation) I am not a big fan of structuring code in a specific way just to make HMR work, but this rule might be a good enough solution for a starter (setting it to Overall it would've been great if |
Edit: moved to this issue function CardContainer(props,ref) {
return <div ref={ref}>Hello world!</div>;
}
export default forwardRef(CardContainer); |
According to the official react-fast-refresh docs:
In react-native and webpack, this rule is implemented using a method that iterates over every export at runtime and checks if it is likely to be a component.
Could we statically check for the same thing to catch cases where this rule is violated?
side note: in typescript code, this rule could be relaxed to allow for type-only exports, such as interfaces, type aliases, or
as const
enums.The text was updated successfully, but these errors were encountered: