-
Notifications
You must be signed in to change notification settings - Fork 17
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
.ts barrel (index) file does not trigger warning #76
Comments
Writing React component without the file being
Nope, if these files respect Fast Refresh rules, the update will not "bubble up" to
Yeah this flag is only for people using JSX inside JS files, which TS does not allow and the reason stated above I don't think having checkTS is a good direction
Barrel files are not great for HMR and mainly for Vite if you are using it, see this talk for an example of the impact it had at Shopify: https://www.youtube.com/watch?v=YcT2yP0nmm0 The only things that you should not do is having React component inside barrel files, but apart from that using them will make HMR ok for most updates and HMR a bit slower when editing non components. Having export {default Cat} from './Cat'
export {default as Bat} from './Bat'
export {default as Rat} from './Rat'
export { SOME_CONSTANT } from './constants'
export { utilA, utilB } from './utils'
export function isThatCatFluffy() {
return "yes"
} does not break fast refresh rules. The only issue is that when doing changes to utilAs, instead of refreshing component that need utilA, it will refresh all components that requires at least one of Cat, Bat, Rat, SOME_CONSTANT, utilA, utilB, isThatCatFluffy (which is why it can slow down HMR). The split in the SO answer is not needed Note: Using default export for React component is really not needed and makes refacotr and re-export just harder for no benefits, I highly advice using Let me know if something is not clear |
Thank you @ArnaudBarre! This is extremely helpful. It sounds like there are some good reasons I should not use barrel files, but that they will not break HMR when used like this. In other words, as long as React component X is defined in a file that only exports React components, HMR will work, even if X is re-exported from a barrel file with mixed exports. One question, though -- let's say that I change the extension of this barrel file to |
Yep correct
Yep if the file does not export anything the match a potential React component, any warning is a false positive and should can be reported to improve the rule. The last release should reduce the number of false positive, but if you have another one feel free to paste a code sample here or in another issue so I can look at it (Note: is you have a PascalCase function |
I have a React component in
Cat.tsx
:I have a barrel file
animals/index.ts
that exports theCat
component, along with a helper function (and potentially other React components):As written, this file will not trigger the react-refresh warning, even though it exports both a React component and a non-React component. If I change the extension to
.tsx
, then the rule fires.Since there's no JSX/TSX in the file, and no React import, there's no inherent reason to use the
.tsx
extension here. I probably have dozens or hundreds of these barrel files around, with a.ts
extension, and if I'm understanding correctly it seems like currently they will break HMR but won't trigger a warning.Questions:
Cat
,Bat
, orRat
? (This is my assumption.)tsx
? (I triedcheckJS
but that didn't seem to do it --maybe we needcheckTS
?)The text was updated successfully, but these errors were encountered: