1. 10
    Clean Up the Folder Structure and Imports in a React Native App with Absolute Paths
    1m 48s

Clean Up the Folder Structure and Imports in a React Native App with Absolute Paths

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 4 years ago

There’s no required folder structure for React Native applications, but we’ll structure our app with some common folder guidelines. Then, we’ll create a package.json file for each sub-folder, which allows us to import components using absolute references like components/Header instead of relative paths like ../../../components/Header.

Instructor: [00:00] There is no defined file or folder structure for React native applications, but our root folder is getting crowded with all of the new files. Let's clean that up by adding some folders.

[00:12] Create a new source folder which we'll use to hold all of the source files. Under source, create a components folder. We'll start by moving the header component files into the components folder.

[00:30] Then let's make a styles folder inside of source. We can move the header style file into the styles folder. Now, if we want to import and use the header, instead of importing header from ./header, we have to use ./source/components/header.

[00:52] In order to avoid the complexity of relative paths, however, we'll first switch to absolute path imports. In the components folder, make a new package.json file. In that package.json, make an object and add a single name key with a value set to components.

[01:12] Now, that components folder will act like a module. Back in app.js, we can import directly from components, which cleans up the import significantly. We can do the same thing with the styles folder by adding a new package.json file and setting the name to styles.

[01:34] Now, in the header.ios and header.android files, we can import the header directly from styles. Now, we can rerun with the new folders and the cleaned up imports.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today