ProNextJS
    Loading
    lesson

    Importing Component Files

    Jack HerringtonJack Herrington

    Picking up from where we left off, we'll start by moving the Button.test.tsx, Button.stories.tsx, and Button.tsx files into the app/Button directory. This way, all the files related to the Button component are in one place.

    the button files have been moved

    Updating Import Paths

    However, when opening page.tsx we can see that the import has been updated to have Button listed twice:

    // inside page.tsx
    import { Button } from './Button/Button';
    

    The goal is to simplify it to:

    import { Button } from './Button'
    

    In order to do this, we can create an index.ts file inside of the Button directory. Inside the file, we'll add an export:

    // inside `src/app/Button/index.ts`
    
    export { Button } from './Button';
    

    Because the component has its own imports, we can make edits like changing CSS colors and the changes will reflect in the application.

    This approach is also nice because we can search for Button and find the source file directly, rather than having to navigate through an index file first.

    Handling Default Exports

    Sometimes, you might have a component defined as a default export. For example, your Button.tsx file might look like this:

    export default function Button() { ... }
    

    In this case, you would need to adjust the index.tsx file slightly to export the default:

    export { default } from './Button'
    

    Advantages of Directory Structure

    Organizing components into directories like this helps make the project structure easier to understand. The directory is also easier to share and reuse across the application or in other projects.

    While it might seem like a lot for smaller components, using this structure from the beginning sets a good foundation and encourages good organization.

    Transcript

    All right, so picking up where we left off, I'm going to go and take our button test, button MDX and button stories, and move those all into button. I'm now going to move the implementation of button into button. All right, so far so good. But if I look over at page, we now have from button button. So button button who's got the button?

    That's not what we want. So we want to just be able to do button. So how are we going to do that? Well we're going to create an index file and then in index we're going to export button from button. Now back over here we can import button and it works just fine.

    Now let's try it out and there we go we've got our buttons on the right hand side. Let's go make a change so we can make sure that that is actually working. We'll go and change the CSS to be red and now they're red. Awesome. Now a question for you, does that still work with things like find the definition?

    So if I go over here to get to go to definition it still goes to definition just fine and now we do command P. If I type in button I actually find the source button. If I had the entire implementation inside of index then when I do my Command Shift P and I looked for a button, I wouldn't find that. I'd have to go to index and then look at the directory name to see if I'm looking at the right component. So I prefer to have this style of having an index that simply just imports and then exports whatever I'm doing inside of say button.

    Now one thing, let's go to a default. So if I do export default function here, how do I handle that? Well, if I go over to my index, then I just change that to default. And now cool, go here and now we've got that and everything's great. So now the great thing here is that everything with button is contained in one spot.

    I can go and take this wherever I want. If I want to make that shared, I can put that up and say it components directory inside source. If I have a monorepo and I have packages UI, I could drag it in there and reuse it there. Everything is contained in one spot. When it comes to debugging or working on it, I know that Everything to do with button is just inside of that button directory.

    It might seem like overkill to have one component per directory, but I think in some cases it's worth thinking about. This is one of those things where if you want to do this, it is a good idea to start out with it. Just kind of get into the rhythm of it. But it's not something where later on if you start with components in single files, that you can't migrate into having components inside of directories.