Tuesday , August 11 2020
Home / Uncategorized / How to structure your project and manage static resources in React Native

How to structure your project and manage static resources in React Native

Source: stmed.net

React and React Native are just frameworks, and do not dictate how we should structure our projects. It all depends on your personal taste and the project you are working on.

In this post, we will examine how to structure a project and how to manage local resources. This is obviously not written in stone, and you are free to apply only the pieces that suit you. I hope you learn something

For a project started with react-native init , we only get the basic structure.

C & # 39; is the ios folder for Xcode projects, the android folder for Android projects, and a index.js it's a App.js file for the React Native starting point.

ios /
Android /

As someone who has worked with native both on Windows Phone, iOS and Android, I find that structuring a project boils down to separating files gender or feature

type vs characteristic

Separating by type means that we organize files according to their type. If it's a component, there are containers and presentation files. If it's Redux, there are actions, reducers and archive files. If it is viewed, there are JavaScript, HTML and CSS files.

Group by type


In this way, we can see the type of each file, and easily execute a script to a certain type of file. This is general for all projects, but does not answer the question "what is this project about?" Is it a news application? Is it a loyalty app? Is it about monitoring nutrition?

Organizing files by type is for a machine, not for a human being. Many times we work on a function and finding files to correct in multiple directories is a hassle. It is also a problem if we plan to create a structure outside our project, because the files are distributed in many places.

Group by feature

A more reasonable solution is to organize files by feature. Files related to a function should be put together. And the test files should stay close to the source files. Take a look at this article to learn more.

A feature can be related to login, registration, integration or a user's profile. A function can contain secondary functions as long as they belong to the same stream. If we wanted to move the secondary function around, it would be easy, since all the related files are already grouped.

My typical design structure based on features is as follows:

ios /
Android /

In addition to traditional files App.js is index.js and the ios1 is android folders, I put all the source files inside the src folder. Inside src I have res for resources, library for common files used through features and screens for a content screen.

As few possible dependencies

Since React Native is heavily dependent on tons of addictions, I try to be quite aware when I add more. In my project I use only react-navigation for browsing. And I'm not a fan of redux as it adds unnecessary complexity. Add an addiction only when you really need it, otherwise you're just standing aside for more problems than for value.

The thing I like about React are the components. A component is where we define vision, style and behavior. React has an online style – it's like using JavaScript to define scripts, HTML and CSS. This adapts to the functionality approach we aim for. That's why I do not use stylized components. Because styles are just JavaScript objects, we can simply share comment styles in library .


I really like Android, so my name is src is res to match its folder conventions.

react-native init organize babel for us. But for a typical JavaScript project, it's good to organize the files in src folder. In my electron.js IconGenerator application, I put the source files inside the src folder. This not only helps in terms of organization, but also helps to transpire the entire folder at once. Just a command and I have the files in src transpired a dist in a blink of an eye.

babel ./src --out-dir ./dist --copy-files


React is based on components. Yes. There are container and presentation components, but we can compose components to build more complex components. They usually end up showing full screen. Is called Page in Windows Phone, ViewController in iOS and Activities in Android. The React Native guide very often mentions the screen as something that covers the entire space:

Mobile apps are rarely made up of a single screen. The management of the presentation and the transition between multiple screens is usually managed by a navigator.

index.js or not?

Each screen is considered the entry point for each function. You can rename the LoginScreen.js to index.js taking advantage of the functionality of the Node module:

The modules must not be files. We can also create a Find me folder below node_modules and place a index.js file there. The same require (& # 39; find-me & # 39;) the line will use that folder index.js file

So instead of import LoginScreen from & # 39; ./ screens / LoginScreen & # 39; we can only do it import LoginScreen from & # 39; ./ screens & # 39;.

using index.js it determines the encapsulation and provides a public interface for the functionality. This is all a personal taste. Personally I prefer to explicitly name a file, hence the name LoginScreen.js.


react-navigation seems to be the most popular choice for managing navigation in a React Native app. For a feature like onboarding, there are probably many screens managed by a navigation stack, so there is no OnboardingNavigator .

You can think of Navigator as something that groups sub-screens or features. Since we group by feature, it is reasonable to place the Navigator inside the function folder. In practice it looks like this:

import {createStackNavigator} from "react-navigation"
import Welcome from & # 39; ./ Welcome & # 39;
Import term from & # 39; ./ Term & # 39;
const routeConfig = {
Welcome: {
screen: welcome
Term: {
screen: Term
const navigatorConfig = {
navigationOptions: {
header: null
export default OnboardingNavigator = createStackNavigator (routeConfig, navigatorConfig)


This is the most controversial part of structuring a project. If you do not like the name library, you can call it utility, Common, citadel , everything

This is not meant for homeless files, but is where the utilities and common components used by many features are placed. Things like atomic components, wrappers, quick fixes, networking elements and access information are used a lot, and it's difficult to move them to a specific function folder. Sometimes we just need to be practical and get the job done.

In React Native, it is often necessary to implement a button with an image background in many screens. Here is a simple one that stays inside library / components / ImageButton.js . The components the folder is for reusable components, sometimes called atomic components. According to the React naming conventions, the first letter must be capitalized.

import React by & # 39; react & # 39;
import {TouchableOpacity, View, Image, Text, StyleSheet} from & # 39; react-native & # 39;
import images from & # 39; res / images & # 39;
import colors from & # 39; res / colors & # 39;
Export the default class ImageButton extends React.Component {
render () {
return (

{} This.props.title

source = {} images.button
style = {styles.image} />

const styles = StyleSheet.create ({
view: {
position: & # 39; absolute & # 39 ;,
backgroundColor: & # 39; transparent & # 39;
Image: {
tangible: {
alignItems: & # 39; center & # 39 ;,
justifyContent: & # 39; center & # 39;
text: {
color: colors.button,
fontSize: 18,
textAlign: & # 39; center & # 39;

And if we want to place the button at the bottom, we use a utility function to prevent duplication of the code. Here is library / utils / moveToBottom.js:

import React by & # 39; react & # 39;
import {View, StyleSheet} from & # 39; react-native & # 39;
function moveToBottom (component) {
return (


const styles = StyleSheet.create ({
container: {
flex: 1,
justifyContent: & # 39; flex-end & # 39 ;,
marginBottom: 36
export default moveToBottom

Use package.json to avoid the relative path

So somewhere in the src / screens / onboarding / term / Term.js , we can import using relative paths:

import moveToBottom from & # 39; ../../../../ library / utils / move & # 39;
Imports ImageButton from & # 39; ../../../../ library / components / ImageButton & # 39;

This is a big red flag in my eyes. It is subject to errors, as it is necessary to calculate how many .. we have to perform. And if we move the functionality, all the paths must be recalculated.

From library It is meant to be used in many places, it is good to refer to it as an absolute path. In JavaScript there are typically 1000 libraries for a single problem. A quick search on Google reveals tons of libraries to address this problem. But we do not need another dependency as it is extremely easy to solve.

The solution is to turn library in a module so node can find it Added package.json to any folder it transforms it into a node module . insert package.json inside the library folder with this simple content:

"name": "library",
"version": "0.0.1"

Now in Term.js we can easily import things from library because now it is a module:

import React by & # 39; react & # 39;
import {View, StyleSheet, Image, Text, Button} from & # 39; react-native & # 39;
import strings from & # 39; res / strings & # 39;
import the palette from & # 39; res / palette & # 39;
import images from & # 39; res / images & # 39;
Imports ImageButton from & # 39; library / components / ImageButton & # 39;
import moveToBottom from & # 39; library / utils / moveToBottom & # 39;
Export the default class Term extends React.Component {
render () {
return (

{Strings.onboarding.term.heading.toUpperCase ()}
moveToBottom (


const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: & # 39; center & # 39;
heading: {... palette.heading, ... {
marginTop: 72


You could ask yourself what res / colors, res / strings , res / images is res / fonts are in the examples above. Well, for front-end projects, we usually have components and we model them using fonts, localized strings, colors, images and styles. JavaScript is a very dynamic language and is easy to use anywhere. We could have a lot of # 00B75D color through many files, or Fira as a font family in many Text components. This is subject to errors and difficult to refactor.

We try to encapsulate the use of resources within the res folder with safer objects. The examples below appear:

res / colors

const colors = {
title: "# 00B75D",
text: "# 0C222B",
button: "# 036675"
exports predefined colors

res / strings

const string =
onboarding: {
welcome: {
header: "Welcome",
text1: "What you do not know is what you have not learned",
text2: "Visit my GitHub at https://github.com/onmyway133",
button: "Log in"
term: {
title: "Terms and Conditions",
button: & # 39; Read & # 39;
export the default strings

res / fonts

const fonts = {
title: & # 39; Arial & # 39 ;,
text: "SanFrancisco",
code: & # 39; Fira & # 39;
export predefined characters

res / images

const images = {
button: require (& # 39; ./ images / button.png & # 39;),
logo: require (& # 39; ./ images / logo.png & # 39;),
placeholder: require (& # 39; ./ images / placeholder.png & # 39;)
export predefined images

Like it library , res files can be accessed from anywhere, so let's do it module . insert package.json to the res folder:

"name": "res",
"version": "0.0.1"

so that we can access resource files like normal forms:

import strings from & # 39; res / strings & # 39;
import the palette from & # 39; res / palette & # 39;
import images from & # 39; res / images & # 39;

Group colors, images, characters with the palette

The app design should be consistent. Some elements should have the same appearance and appearance so as not to confuse the user. For example, the item Text it should use a color, a font and a font size. The Image component should use the same placeholder image. In React Native, we already use the name styles with const styles = StyleSheet.create ({}) so we use the name palette.

Below is my simple palette. Defines common styles for header and Text:

res / pallet

import colors from & # 39; ./ colors & # 39;
const palette = {
heading: {
color: colors.title,
fontSize: 20,
textAlign: & # 39; center & # 39;
text: {
color: colors.text,
fontSize: 17,
textAlign: & # 39; center & # 39;
export the default palette

And then we can use them on our screen:

const styles = StyleSheet.create ({
container: {
flex: 1,
alignItems: & # 39; center & # 39;
heading: {... palette.heading, ... {
marginTop: 72

Here we use the object spread operator to merge palette.heading and our personalized style object. This means that we use styles from palette.heading but also specify more properties.

If we had to go down the app for several brands, we could have more palettes. This is a really powerful model.

Generate images

You can see it inside /src/res/images.js we have the properties for each image in the src / res / images folder:

const images = {
button: require (& # 39; ./ images / button.png & # 39;),
logo: require (& # 39; ./ images / logo.png & # 39;),
placeholder: require (& # 39; ./ images / placeholder.png & # 39;)
export predefined images

This is boring to do manually, and we have to update ourselves if there are changes in the image naming convention. Instead, we can add a script to generate the images.js based on the images we have. Add a file to the root of the project /scripts/images.js:

const fs = require (& # 39; fs & # 39;)
const imageFileNames = () => {
const array = fs
.readdirSync (& # 39; src / RES / images & # 39;)
.filter ((file) => {
return file.endsWith (& # 39 ;. png & # 39;)
.map ((file) => {
return file.replace (& # 39;@ 2x.png & # 39;, & # 39; & # 39;) .replace (& # 39;@ 3x.png & # 39;, & # 39; & # 39;)
return Array.from (new Set (array))
const generate = () => {
let properties = imageFileNames ()
.map ((name) => {
return `$ {name}: require (& # 39; ./ images / $ {name} .png & # 39;)`
.join (& # 39 ;, n & # 39;)
const string = `const images = {
$ {} property
export predefined images
fs.writeFileSync (& # 39; src / res / images.js & # 39 ;, string, & # 39; utf8 & # 39;)

The nice thing about Node is that we have access to the fs module, which is really good at processing files. Here we simply cross the images and update /src/res/images.js Consequently.

Whenever we add or change images, we can perform:

node / images.js script

And we can also declare the script inside our main package.json :

"script": {
"start": "node node_modules / react-native / local-cli / cli.js start",
"test": "jest",
"lint": "eslint * .js ** / *. js",
"images": "node scripts / images.js"

Now we can only run npm performs images and we get an update images.js resource file.

The namespace R

This passage depends on personal taste, but I find it more organized if we introduce the namespace R, just as Android does for resources with the generated R class.

Once you have outsourced the app resources, you can access them using the resource IDs generated in the projects Rclass. This document shows how to group resources in the Android project and provide alternative resources for specific device configurations, then access them from the app code or other XML files.

In this way, we make a file called R.js in src / library:

import strings from & # 39; ./ strings & # 39;
import images from & # 39; ./ images & # 39;
import colors from & # 39; ./ colors & # 39;
import the palette from & # 39; ./ palette & # 39;
const R = {
default export R

And access it on the screen:

imports R from & # 39; res / R & # 39;
render () {
return (

style = {} styles.logo
source = {R.images.logo} />
style = {} styles.image
source = {R.images.placeholder} />
{R.strings.onboarding.welcome.title.toUpperCase ()}

Replace strings with R.strings, colors with r.colors, is images with R.images. With the R annotation, it is clear that we are accessing the static resources from the app package.

This also corresponds to the Airbnb singleton convention, as our R is now a global constant.

23.8 Use PascalCase when exporting a constructor / class / singleton / function library / naked object.

const AirbnbStyleGuide = {
es6: {

export default AirbnbStyleGuide

Where to go from here

In this post, I showed you how I think you should structure folders and files in a React Native project. We have also learned how to manage resources and access them more securely. I hope you found it useful. Here are some other resources to explore further:

Source link


  1. Great line up. We will be linking to this great article on our site. Keep up the good writing.

  2. You really make it seem really easy together with your presentation however I to find this matter to be actually one thing which I think I’d never understand. It seems too complex and very wide for me. I’m having a look ahead on your next submit, I will try to get the grasp of it!

  3. Howdy! I know this is somewhat off topic but I was wondering which blog platform are you using
    for this site? I’m getting sick and tired of WordPress because I’ve had problems with hackers and I’m looking
    at alternatives for another platform. I would be fantastic
    if you could point me in the direction of a good platform.

  4. Whats up this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.

    I’m starting a blog soon but have no coding skills so
    I wanted to get advice from someone with experience.

    Any help would be enormously appreciated!

  5. Good day! This is kind of off topic but I need some advice
    from an established blog. Is it very difficult to set up your own blog?
    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about setting up my own but I’m not sure where to begin. Do you have any tips or suggestions?
    Thank you

  6. Hi, i think that i saw you visited my web site so i got here to go back
    the favor?.I’m attempting to find issues to improve my web site!I assume its
    good enough to make use of a few of your concepts!!

  7. Helpful info. Fortunate me I discovered your web site by chance, and I am surprised why this accident didn’t took place in advance!
    I bookmarked it.

  8. Hi this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding knowledge so I wanted to get guidance from someone with experience.
    Any help would be greatly appreciated!

  9. I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit
    more often. Did you hire out a designer to create your theme?
    Fantastic work!

  10. Admiring the time and energy you put into your blog and in depth information you present.
    It’s awesome to come across a blog every once in a while that isn’t the same unwanted rehashed material.
    Excellent read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  11. Hi there, for all time i used to check webpage posts here early in the morning,
    since i love to learn more and more.

  12. I have read so many articles about the blogger lovers except this post is actually a fastidious post, keep
    it up.

  13. Good day! Would you mind if I share your blog with
    my myspace group? There’s a lot of people that I think would really appreciate your content.
    Please let me know. Thank you

  14. Hello There. I found your weblog the use of msn. That
    is an extremely well written article. I will make sure to bookmark it and come back to read extra of your useful information. Thanks for the post.
    I will certainly return.

  15. Magnificent goods from you, man. I’ve understand your stuff previous to and you’re just too fantastic.
    I actually like what you’ve acquired here, certainly
    like what you are stating and the way in which you say it.
    You make it entertaining and you still take care of to
    keep it wise. I cant wait to read much more from you. This is actually a great web site.

  16. Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something.
    I think that you can do with a few pics to drive the
    message home a bit, but other than that, this is great blog.
    A great read. I’ll certainly be back.

  17. Hello, just wanted to mention, I liked this article. It was helpful.
    Keep on posting!

  18. Excellent article! We are linking to this particularly great content
    on our website. Keep up the great writing.

  19. I always emailed this webpage post page to all my contacts, since if
    like to read it then my links will too.

  20. I pay a visit day-to-day some web pages and blogs to read articles or reviews, however
    this weblog offers feature based content.

  21. Howdy! This article couldn’t be written much better!

    Reading through this post reminds me of my previous roommate!
    He always kept preaching about this. I will forward this information to him.
    Fairly certain he’s going to have a good read.
    Thank you for sharing!

  22. Hi there to every one, because I am genuinely eager of reading this webpage’s post to be updated on a regular
    basis. It includes nice data.

  23. With havin so much content and articles do you ever run into any
    issues of plagorism or copyright infringement? My site has
    a lot of completely unique content I’ve either authored myself or
    outsourced but it appears a lot of it is popping it up all over
    the web without my agreement. Do you know any techniques to help protect against content from being ripped off?
    I’d certainly appreciate it.

  24. Greetings! Very useful advice within this post!
    It is the little changes that make the biggest changes.
    Thanks for sharing!

  25. This is a good tip particularly to those new to the blogosphere.
    Simple but very precise information… Many thanks for sharing this one.
    A must read article!

  26. Amazing blog! Do you have any tips for aspiring writers?

    I’m hoping to start my own blog soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress or
    go for a paid option? There are so many options out there that I’m totally confused ..
    Any tips? Bless you!

  27. Yes! Finally something about coconut oil.

  28. Hi there to all, the contents present at this site are actually amazing for people knowledge, well, keep up the
    nice work fellows.

  29. Hey I am so thrilled I found your webpage, I really found you by accident, while I was looking
    on Yahoo for something else, Regardless I am here now and would just
    like to say many thanks for a fantastic post and a all round entertaining blog (I also love the theme/design),
    I don’t have time to read it all at the moment but
    I have saved it and also added in your RSS feeds, so when I have time
    I will be back to read a great deal more, Please do keep
    up the superb b.

  30. Yes! Finally someone writes about match.com free trial.

  31. Woah! I’m really enjoying the template/theme of this blog.
    It’s simple, yet effective. A lot of times it’s
    hard to get that “perfect balance” between usability and visual appeal.
    I must say that you’ve done a amazing job with this. In addition, the blog
    loads extremely quick for me on Firefox. Outstanding Blog!

  32. It’s in point of fact a nice and useful piece of information. I’m satisfied that you shared this
    helpful information with us. Please keep us informed like this.

    Thank you for sharing.

  33. You really make it appear really easy with your presentation but I in finding this topic to be really
    something which I believe I might by no means understand.
    It kind of feels too complex and very large for
    me. I am having a look forward on your subsequent submit, I will try to get the dangle of it!

  34. What’s up friends, its enormous article on the topic of tutoringand fully explained, keep it up all the time.

  35. Hi there, I enjoy reading through your post. I wanted to write a little comment to support you.

  36. You should take part in a contest for one of the most useful websites online.
    I am going to highly recommend this blog!

  37. bookmarked!!, I like your blog!

  38. You are so awesome! I do not believe I’ve truly read through something like this before.

    So good to find someone with unique thoughts on this issue.

    Seriously.. many thanks for starting this up.
    This site is something that is required on the internet, someone
    with a bit of originality!

  39. What’s up it’s me, I am also visiting this
    site regularly, this site is actually good and the people are genuinely sharing
    good thoughts.

  40. Have you ever thought about including a little bit more
    than just your articles? I mean, what you say is important and all.
    However think of if you added some great graphics or videos to give your posts more, “pop”!
    Your content is excellent but with images and videos,
    this website could definitely be one of the greatest in its field.
    Amazing blog!

  41. Hi there! I know this is kind of off topic but I was wondering which blog platform are you using for this site?
    I’m getting sick and tired of WordPress because I’ve had issues with hackers and
    I’m looking at alternatives for another platform. I would
    be fantastic if you could point me in the direction of a good platform.

  42. Whoa! This blog looks exactly like my old one! It’s on a completely different topic but it has
    pretty much the same page layout and design. Great choice of colors!

  43. Hello there, I found your blog by the use of Google at the same time as looking for a
    comparable subject, your website came up, it looks good.
    I’ve bookmarked it in my google bookmarks.

    Hi there, just turned into alert to your weblog thru Google, and found
    that it is really informative. I am going to be careful for brussels.
    I’ll be grateful if you happen to continue this in future.
    Lots of folks might be benefited out of your writing.

  44. I was excited to uncover this web site. I wanted to thank you
    for ones time due to this fantastic read!!
    I definitely really liked every little bit of it and I have you saved as a favorite
    to see new information in your web site.

Leave a Reply

Your email address will not be published.