<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tamas Menyhart</title>
    <description>The latest articles on Forem by Tamas Menyhart (@tmenyhart).</description>
    <link>https://forem.com/tmenyhart</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F449971%2F755545fc-9c1b-4588-b316-3f1c870917c5.jpeg</url>
      <title>Forem: Tamas Menyhart</title>
      <link>https://forem.com/tmenyhart</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tmenyhart"/>
    <language>en</language>
    <item>
      <title>Reactive styles in React Native</title>
      <dc:creator>Tamas Menyhart</dc:creator>
      <pubDate>Thu, 18 Feb 2021 14:12:16 +0000</pubDate>
      <link>https://forem.com/tmenyhart/reactive-styles-in-react-native-38kb</link>
      <guid>https://forem.com/tmenyhart/reactive-styles-in-react-native-38kb</guid>
      <description>&lt;p&gt;In the age of light / dark themes and white label applications, it’s always a good idea to think over the theming of mobile applications...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/supercharges-mobile-product-guide/reactive-styles-in-react-native-79a41fbdc404"&gt;Continue reading&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>style</category>
      <category>darktheme</category>
    </item>
    <item>
      <title>Pro tips for setting up a React Native project in 2020</title>
      <dc:creator>Tamas Menyhart</dc:creator>
      <pubDate>Wed, 21 Oct 2020 12:15:05 +0000</pubDate>
      <link>https://forem.com/tmenyhart/pro-tips-for-setting-up-a-react-native-project-in-2020-j00</link>
      <guid>https://forem.com/tmenyhart/pro-tips-for-setting-up-a-react-native-project-in-2020-j00</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4hTKmiDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/95y6hdv1nnjc4w1ou81h.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4hTKmiDB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/95y6hdv1nnjc4w1ou81h.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="//bit.ly/2TeLA0Y"&gt;Medium&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;The year is 2020 and mobile development is still in an upturn. &lt;em&gt;For smaller businesses native mobile development is not an option most of the time because it requires a lot of time and developer resources so it is not cost effective enough&lt;/em&gt;. Luckily, React Native, Facebook's mobile cross platform development tool have improved a lot in the past years and it can be a viable option instead of pure native mobile development.&lt;/p&gt;

&lt;p&gt;In this article I will discuss the following topics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Motivation&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Project bootstrapping&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Initial directory and file changes&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Explicit build tool and dependency versions&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Code formatting and linting&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Absolute imports and modularization&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Grouped exports&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Final thoughts&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Motivation
&lt;/h1&gt;

&lt;p&gt;Personally, I like to plan ahead, I'm convinced that if a project builds on a solid base then it will stand the test of time. Whenever I start working on a project, I always assume it is a long running one, meaning I try to prepare for whatever might happen but without being prophetic.&lt;/p&gt;

&lt;p&gt;As at &lt;a href="https://supercharge.io/"&gt;Supercharge&lt;/a&gt; I had the opportunity to successfully ship multiple React Native projects and support them for years in production, I would like to share with you some tips on how I would bootstrap a new React Native project nowadays.&lt;/p&gt;

&lt;h1&gt;
  
  
  Prerequisites
&lt;/h1&gt;

&lt;p&gt;Other than the &lt;a href="https://reactnative.dev/docs/environment-setup"&gt;React Native requirements&lt;/a&gt;, I recommend installing Node Version Manager (&lt;a href="https://github.com/nvm-sh/nvm"&gt;MacOS&lt;/a&gt;, &lt;a href="https://github.com/nvm-sh/nvm"&gt;Linux&lt;/a&gt;, &lt;a href="https://github.com/coreybutler/nvm-windows"&gt;Windows&lt;/a&gt;), NodeJS 8.17.0 or newer, Node Package Manager 5.2 or newer, and &lt;a href="https://code.visualstudio.com/"&gt;Visual Studio Code&lt;/a&gt; with &lt;a href="https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint"&gt;ESLint&lt;/a&gt; and &lt;a href="https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode"&gt;Prettier&lt;/a&gt; extensions.&lt;/p&gt;

&lt;h1&gt;
  
  
  Project bootstrapping
&lt;/h1&gt;

&lt;p&gt;I always use the &lt;a href="https://github.com/react-native-community/cli"&gt;React Native CLI&lt;/a&gt; to create a new React Native project, because &lt;em&gt;in the long term there's a great chance that I have to write some native (Android, IOS) code&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So let's create our new React Native project called &lt;em&gt;MyApp&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx react-native init MyApp --template react-native-template-typescript --npm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;a href="https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b"&gt;NPX&lt;/a&gt; is a package runner for NPM&lt;/strong&gt;, and it's available since NPM 5.2. &lt;em&gt;With NPX there's no need to install the 'react-native-cli' package globally&lt;/em&gt; just to initialize our project.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;'--template react-native-template-typescript'&lt;/strong&gt; parameter will &lt;em&gt;use the Typescript template to create the project&lt;/em&gt;. I always use Typescript for static type checking, it will make things more developer friendly and less prone to errors.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;'--npm'&lt;/strong&gt; parameter will make the project &lt;em&gt;use NPM instead of Yarn to install initial dependencies&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Initial directory and file changes
&lt;/h1&gt;

&lt;p&gt;I like to move all of the project javascript &lt;strong&gt;source files to an 'src' directory&lt;/strong&gt; to keep the project root nice and tidy. Then I &lt;strong&gt;move the 'App.tsx' file from the project root to the 'src' directory&lt;/strong&gt;. Finally, because we're using Typescript in the project, I &lt;strong&gt;rename the 'index.js' file to 'index.ts'&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Explicit build tool and dependency versions
&lt;/h1&gt;

&lt;p&gt;It's important to make project builds repeatable, no matter where the project is built (CI, other developer's machine). That's why I like to use explicit build tools, package manager and dependency versions wherever possible.&lt;/p&gt;

&lt;p&gt;To use &lt;strong&gt;explicit Node and NPM versions&lt;/strong&gt;, some changes need to be made:&lt;/p&gt;

&lt;p&gt;Update &lt;strong&gt;package.json&lt;/strong&gt; file with 'engines' block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"engines": {
  "node:": "x.y.z",
  "npm": "a.b.c",
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Where &lt;strong&gt;"x.y.z"&lt;/strong&gt; is the &lt;strong&gt;explicit Node version&lt;/strong&gt; you want to use (Eg: "12.16.1")&lt;/li&gt;
&lt;li&gt;Where &lt;strong&gt;"a.b.c"&lt;/strong&gt; is the &lt;strong&gt;explicit NPM version&lt;/strong&gt; you want to use (Eg: "6.13.4")&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a &lt;strong&gt;.npmrc file in the project root directory&lt;/strong&gt; with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;engine-strict=true
save-exact=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The '&lt;strong&gt;engine-strict=true&lt;/strong&gt;' line will make &lt;em&gt;NPM look for allowed version defined in the "engines" block&lt;/em&gt; when I am trying to install dependencies&lt;/li&gt;
&lt;li&gt;The '&lt;strong&gt;save-exact=true&lt;/strong&gt;' line will make &lt;em&gt;NPM install explicit dependency versions&lt;/em&gt;. You're correct to think that's why we have the 'package-lock.json' file, but I like to see explicit dependency versions in the package.json file as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Code formatting and linting
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;consistent code style&lt;/strong&gt; is really important when multiple developers contribute to the project. &lt;em&gt;To make code formatting become nothing to worry about, I use Prettier, which takes the burden off my shoulders&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There's a &lt;strong&gt;'.prettierrc.js' file in the project root&lt;/strong&gt; already, but we can customize it further. In my case I use the following Prettier config:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  tabWidth: 2,
  useTabs: false,
  printWidth: 140,
  semi: true,
  trailingComma: 'es5',
  bracketSpacing: true,
  arrowParens: 'always',
  singleQuote: true,
  jsxSingleQuote: true,
  jsxBracketSameLine: true,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To &lt;strong&gt;make code formatting an automatic task on save&lt;/strong&gt;, I populate the workspace config file of Visual Studio Code (&lt;strong&gt;.vscode/settings.json&lt;/strong&gt;) with the following content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "editor.tabSize": 2,
  "editor.formatOnSave": false,
  "[javascript]": {
    "editor.formatOnSave": true
  },
  "[typescript]": {
    "editor.formatOnSave": true
  },
  "[javascriptreact]": {
    "editor.formatOnSave": true
  },
  "[typescriptreact]": {
    "editor.formatOnSave": true
  },
  "typescript.tsdk": "node_modules/typescript/lib"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As &lt;strong&gt;I prefer single quotes&lt;/strong&gt; in the source files, I &lt;strong&gt;add rule override to the '.eslintrc.js'&lt;/strong&gt; file to not to warn me about single quotes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  root: true,
  extends: '@react-native-community',
  parser: '@typescript-eslint/parser',
  plugins: ['@typescript-eslint'],
  overrides: [
    {
      files: ['*.js', '*.ts', '*.jsx', '*.tsx'],
      rules: {
        'jsx-quotes': 0,
      },
    },
  ],
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a final step, I &lt;strong&gt;add linting scripts to the package.json file&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "lint:style": "prettier --check 'src/**/**.{js,ts,jsx,tsx}'",
  "lint:code": "eslint src/ --ext .js,.jsx,.ts,.tsx",
  "lint": "npm run lint:style &amp;amp;&amp;amp; npm run lint:code",
  "test": "jest",
  "sanity": "npm run lint &amp;amp;&amp;amp; npm run test"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Absolute imports and modularization
&lt;/h1&gt;

&lt;p&gt;I don't like &lt;em&gt;long relative imports&lt;/em&gt; when I'm deep in the directory tree. It &lt;em&gt;makes refactoring circumstantial&lt;/em&gt;. In order to avoid this, I always &lt;strong&gt;modularize the codebase based on some logic&lt;/strong&gt; and &lt;strong&gt;use absolute imports&lt;/strong&gt; in the source files.&lt;/p&gt;

&lt;p&gt;First, let's create the following directory and file structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  app/
    App.tsx
    package.json
  core/
    package.json
  ui/
    package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;By creating package json files in the sub directories, I define sub-modules&lt;/strong&gt;. Let's populate the contents of the package.json files:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/app/package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "app"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/core/package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "core"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/ui/package.json&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "ui"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To be able to use absolute imports, I need to update the tsconfig.json file in the project root:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "compilerOptions": {
    "baseUrl": "./src",
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After these changes &lt;strong&gt;we can use import statements the following way&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/app/App.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Row } from 'ui/component/layout/Row.component.tsx'
import { someExportedUtilMethod } from 'core/util/utils.ts'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To avoid circular imports and keep the separation of concerns intact, there are some important rules to pay attention to:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Files under any sub-module directory should use relative imports when importing from files under the same sub-module directory&lt;/li&gt;
&lt;li&gt;Files under the 'app' directory should import from files under the 'app', 'core' and 'ui' directories&lt;/li&gt;
&lt;li&gt;Files under the 'core' directory should only import from files under the 'core' directory&lt;/li&gt;
&lt;li&gt;Files under the 'ui' directory should import from files under the 'ui' and 'core' directories&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Grouped exports
&lt;/h1&gt;

&lt;p&gt;When &lt;strong&gt;named exports are used instead of default exports, they can be grouped in index files&lt;/strong&gt; which then can be used to &lt;strong&gt;import the named exports from&lt;/strong&gt;. The advantage of this is that &lt;em&gt;files can be moved freely under a given root directory and there is no need to update every single source file where these files are used to import from&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;First, I have the following directory and file structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;src/
  app/
    App.tsx
    package.json
  core/
    package.json
  ui/
    component/
      button/
        index.ts
        FlatButton.component.tsx
        RaisedButton.component.tsx
      layout/
        index.ts
        Column.component.tsx
        Row.component.tsx
      index.ts
    package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/ui/component/button/index.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from './FlatButton.component';
export * from './RaisedButton.component';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/ui/component/layout/index.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from './Column.component';
export * from './Row.component';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;src/ui/component/index.ts&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from './button';
export * from './layout';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After I've populated the index files, I can use imports the following way:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;src/app/App.tsx&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Column, FlatButton, RaisedButton, Row } from 'ui/component';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;This way I don't have to explicitly define the path for the import&lt;/strong&gt;, &lt;em&gt;I can move the components under the component directory freely&lt;/em&gt;, I only have to update the related index files.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final thoughts
&lt;/h1&gt;

&lt;p&gt;I hope this bootstrapping guide will be helpful for you in the future when starting a new React Native project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember, nothing is written in stone, these are just suggestions based on my own experience with React Native.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading it through, I welcome any feedback regarding this topic.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;At &lt;a href="https://supercharge.io/"&gt;Supercharge&lt;/a&gt;, we are a next-generation innovation agency working with our clients to create transformative digital solutions. If you liked this article, check out some of Supercharge's other articles on our &lt;a href="https://supercharge.io/blog"&gt;blog&lt;/a&gt;, or follow us on &lt;a href="https://www.linkedin.com/company/teamsupercharge/"&gt;LinkedIn&lt;/a&gt;, and &lt;a href="https://www.facebook.com/TeamSupercharge"&gt;Facebook&lt;/a&gt;. If you're interested in open positions, follow this &lt;a href="https://supercharge.io/jobs"&gt;link&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>reactnative</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
