Delightful React file/directory structure
11–20 of 53 posts
Re: Delightful React file/directory structure
#12Earlier quoted context omitted.
I think his criticism of that approach is that you end up with a bunch of index.tsx tabs open in your IDE and it can be hard to tell what is what. My approach is similar to his, but is simplified to have one index at the top of the components folder for all the components: components index.tsx Then import is easy, and feels less redundant import { mywidget, mycomplexwidget } from '@foo/components'
That issue with too many index.ts files used to be a major issue but nowadays I use VS Code which disambiguates matching filenames with their parent directory. I think I just don't really like using re-exporting index files if I can avoid them, because I don't see much of a positive cost/benefit in most cases. In your example I'd be doing something like this instead: import { mywidget } from '@foo/components/mywidget…
Any source on that. I'm in vscode right now with several index.tsx files open and no disambiguation in the tabs.
Re: Delightful React file/directory structure
#13Earlier quoted context omitted.
That issue with too many index.ts files used to be a major issue but nowadays I use VS Code which disambiguates matching filenames with their parent directory. I think I just don't really like using re-exporting index files if I can avoid them, because I don't see much of a positive cost/benefit in most cases. In your example I'd be doing something like this instead: import { mywidget } from '@foo/components/mywidget…
> That issue with too many index.ts files used to be a major issue but nowadays I use VS Code which disambiguates matching filenames with their parent directory. Any source on that. I'm in vscode right now with several index.tsx files open and no disambiguation in the tabs.
it's controlled by the "workbench.editor.labelFormat" setting, but the default will disambiguate when two files share a name.
Re: Delightful React file/directory structure
#14 src/
components/
Post.jsx
Post/
PostHeader.jsx
PostActions.jsx
Then import as: import Post from “components/Post.js”
And within the component: import PostActions from “components/Post/PostAction.js”
I’ve felt this approach eliminates most index.js reexports and aligns closer with a browser’s native import syntax. This all comes at the cost of less encapsulation, but in a private codebase, it’s less of an issue.Re: Delightful React file/directory structure
#15I would avoid default exports at all cost.
Re: Delightful React file/directory structure
#16I would avoid default exports at all cost.
There are advantages with default exports, e.g. you can name it at usage site the way you want without the `:` syntax. Also some things like `React.lazy` only works with default exports.
Re: Delightful React file/directory structure
#17In my experience, writing a game in mostly React, separating things by feature is more intuitive down the line. Mostly because after some time has passed, finding something is pretty easy and I don't get completely lost in code. And yes, this means having custom hooks in the same file as the actual component sometimes.
I also purposefully let convoluted import statements with lots of '../../../' just exist because my IDE (VS Code) takes care of it and if you really think about it, you never really spend too much time on them.
Then again, I've come to the realization that different things work for different folks. And different projects. If anything, I just encourage more people to try different structures until something 'clicks' with you.
Re: Delightful React file/directory structure
#18Kinda wish Josh would mention how he structures his tests in his projects. Something I'm currently struggling at work is that within our code repos, the pattern that everyone seems to copy is mimic the src/ directory for the test/ directory, rather than co-locating tests along with components. This means a structure for components: src/ components/ Button/ Button.tsx Is just copied ad-hoc for tests, so we get this: t…
I think people do the "separate directory for tests" thing because test runners have, in the past, shipped with a default configuration to target a test directory, rather than match test files by suffix. Colocation of test files is the hands down winner and encourages the writing of tests. When you're making a change to a code file, you probably won't think to scour the codebase for relevant tests. If you see the tes…
Re: Delightful React file/directory structure
#19Kinda wish Josh would mention how he structures his tests in his projects. Something I'm currently struggling at work is that within our code repos, the pattern that everyone seems to copy is mimic the src/ directory for the test/ directory, rather than co-locating tests along with components. This means a structure for components: src/ components/ Button/ Button.tsx Is just copied ad-hoc for tests, so we get this: t…
Re: Delightful React file/directory structure
#20 src/
App/
screens/
Login/
component.tsx
index.tsx
model.tsx
styles.scss
Dashboard/
SomeDashboardPart/
component.tsx
index.tsx
SomeOtherDashboardPart/
component.tsx
index.tsx
helpers/
someHelper.tsx
component.tsx
index.tsx
model.tsx
components/
Button/
component.tsx
index.tsx
`model.tsx` files here are MobX data models used for this specific component