Categories
React Native

Learn React Native with a Board Game (Part 1 of 4)

In this tutorial, you’ll be learning about basic concepts, developing an application from scratch, state, props, and components. This tutorial is for both beginners and professionals.

Introduction to React Native

React Native is a JavaScript framework created by Facebook, Inc. It is used to develop mobile applications for Android and iOS. The mobile applications are developed only using JavaScript, and it is supported by both iOS and Android. It uses native components instead of web components as building blocks. We just put those building blocks together using React and JavaScript.

React Native is not used for developing web applications or an HTML application; it is only used to develop real, native mobile applications that are indistinguishable from mobile applications built using Java or C++. The apps developed using React Native does not load on any browsers, they run on a mobile device only.”

Prerequisite

For better understanding, you must have the basic knowledge of HTML and CSS, OOPs concepts, and JavaScript.

Advantages of React Native

  • Cross-Platform: It works for both Android and iOS devices.
  • JavaScript: The knowledge of JavaScript can be used to build native mobile apps.
  • Reloading: Changes made in the code of the app will be visible during development.

Limitations of React Native

  • Updates: Few components of Android and iOS are not supported.
  • Native Components: If the developer wants to create native functionality that is yet to be created, then the developer has to write platform-specific code.

React Native Environment Setup

The instructions and environment setup are quite different for each operating system. Here, we have chosen Windows as our development OS and Android as our target OS.

To set up the React Native environment, you have to download and install NodeJS, React Native CLI, Android Studio, Python, and JDK software.

Steps to Setup React Native CLI Environment

Step 1: Install NodeJS and NPM (Node Package Manager)

Click here to download NodeJS. NPM comes with NodeJS.

To verify whether NodeJS and NPM are installed or not, open the command prompt and type “node -v” and hit enter. This command will give the version of NodeJS installed.

Next, type “npm -v” and hit enter, this command will give you the version of NPM installed.

Note: Before going to the next step, make sure that you have installed Python, JDK, NPM, and NodeJS software on your system.

Step 2: Install React Native CLI

Open the command prompt and run the below code:

npm install -g react-native-cli

This command will install React Native.

To install Expo CLI use the below command.

npm install -g expo-cli

Note: We’ll discuss Expo CLI later in this post.

Download and setup Android studio

Step 1: Download the latest version of Android Studio from “https://developer.android.com/studio.
Step 2: Go to the download folder, double click on the downloaded android studio set (.exe) file. In the Welcome to Andriod Studio Setup Window click Next.

Step 3: Make sure the Android virtual device is checked. Click Next.

Step 4: Choose the destination folder and click Next.


Step 5: Follow the prompts and click install.

Step 6: Let the installation complete and click Next.

Step 7: Click Finish.

Step 8: Click Next, select an installation type and click Next.

Step 9: Select the UI theme and click Next.

Step 10: In the Verify Settings Window, click Finish.

Step 11: In the Downloading components Window, click Finish.

Configuring AVD Manager

Once Android Studio is installed, we’re going to configure the AVD Manager.
Step 1: At the bottom left of the Android Studio Window, you’ll find a configure option, click on it, and select AVD Manager.

Step 2: Click “Create Virtual Device.”

Step 3: Choose a device, click Next.

Step 4: Download any Android version for your Virtual Device. Click download, accept the license agreement. Click Next.

Step 5: Once the Android version is downloaded on a virtual device, click Next.

Step 6: Give a name to the AVD and click Finish.

Step 7: Click on the Play icon under the Actions column to start your AVD.

Hurray! You have completed the React Native Environment Setup. Create a new project and start working.

Expo

In this guide, we’ll teach you how you can create your first app, installing tools, and give you an overview of how to work with Expo tools, limitations, and environment setup.

The easiest way to get started with mobile development is with Expo CLI. Expo is a set of tools built around React Native. Expo allows you to build universal real native apps using JavaScript. To build apps using Expo CLI, all you need is a recent version of NodeJS, Android Studio, and an emulator or phone.

Expo allows you to run an app on a physical device without setting up a development environment. To run the React Native app on your physical device, all you have to do is to install the Expo application from the play store and scan the QR code displayed on Command Line Interface.

Limitations of Expo

  • If your app wants a specific thing, Expo may not support it.
  • Many features of iOS and Android APIs are still not available, for example, Bluetooth is not available.
  • Does not support the background execution of the code.
  • Only minimum versions of iOS and Android are supported.
  • The managed workflow may not be the best option, to keep the size of the app too less.

Advantages

  • Better development experience.
  • Easy to upgrade to newer versions.
  • Easy to deploy the application in Apple or Google store.
  • Setup takes less time.

Difference between Expo CLI and React Native CLI

Expo CLI

  • No access to Native Applications.
  • Expo CLI makes the development process smoother.
  • Bigger App size.
  • Android Studio or Xcode is not required.
  • The code is almost the same for both.

React Native CLI

  • Have access to Native applications.
  • The development process is not smoother.
  • Small App size.
  • Android Studio or Xcode is required.
  • The code is almost the same.

Expo CLI Installation and Setup

Step 1:

npm install -g expo-cli

The above command will install the Expo CLI.

To check the installed version of the Expo CLI, enter the below command in the command prompt and hit enter.

expo -- version

Step 2: Create a new project. Use the below command to create a new project.

expo init my-project

Note: In place of my-project you can give any name.

Step 3: Navigate to the project directory and type the below command to open iOS or Android.

Hit enter after each command.

cd my-project
npm start 

Step 4: Now, the Expo dev tool will automatically get started.

Now, if you wish to connect your physical device, then download the Expo app (Android) or the camera app (iOS) on your physical device and scan the QR code displayed in your CLI.

If you wish to use Android emulator, press a, or w to run on web.

If you press a, then it will start downloading and installing Expo on your AVD.

Hurray! You have completed the Expo Installation, Setup, Creating a new project. You can start working on it now.

Categories
IntelliJ MySQL

IntelliJ – Connect to MySQL running in Vagrant

The Database feature of IntelliJ/PHPStorm is very powerful compared to the MySQL CLI. It allows your edit your data from the UI easily without writing any MySQL commands.

Here is how you can connect your MySQL server running in a vagrant machine to the IntelliJ’s database feature and improve your productivity during development.

Create a new MySQL Data Source with these basic configuration:

Fill the basic details such as the data source name, host, port, user name, password and the database name.

Make sure you add ?useSSL=false to the JDBC URL after the form is filled. MySQL 5.7+ has SSL enabled by default.

Full JDBC url: jdbc:mysql://localhost:3306/development_database?useSSL=false

Now, with this default configuration, the connection will not work because we need to set the SSH configuration to allow IntelliJ to talk to the MySQL server running within the vagrant VM.

Goto the SSH/SSL tab, select ‘Use SSH tunnel’ and fill-in the details like this:

The value for Private key file should be the one from IdentityFile from your project vagrant directory using this command:

~/Dropbox/Projects/myproject
❯ vagrant ssh-config
Host default
  HostName 127.0.0.1
  User vagrant
  Port 2222
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile /Users/lrajasekaran/Dropbox/Projects/myproject/.vagrant/machines/default/virtualbox/private_key/private_key
  IdentitiesOnly yes
  LogLevel FATAL

Now click ‘Test Connection’ from this dialog to ensure the SSH connection is working as expected. Once the SSH connection is setup, close this dialog, go to ‘General’ tab and click ‘Test Connection’ to test the actual database connection using the SSH connection made previously. Once this successful, goto ‘Schemas’ tab and select your database name and click ‘Apply’.

Now you can view all your tables from the Database tool window on the right. Right click on any table and click ‘Jump to Editor’ to see the table rows:

Any value can be edited directly from this UI and then saved to the DB by right clicking the row and clicking ‘Submit’

Categories
React

React – Render String with HTML tags as HTML

TRY IT LIVE

Click the button below to see a live example of this example that you can edit and try for yourself.

CODE SANDBOX

There are often times when you have a string with some HTML tags such as strong that you want to render as HTML on the DOM. Most solutions online recommend using dangerouslySetInnerHTML but that is dangerous as the name suggests. The proper way to render HTML from string is to use FormattedMessage from the formatjs library (react-intl version 5).

Here is a code example:

import React from "react";
import "./styles.css";
import { IntlProvider, FormattedMessage } from "react-intl";

export default function App() {
  return (
    <IntlProvider>
      <div className="App" style={{fontSize: 24}}>
        <FormattedMessage
          id="app.greeting"
          description="Bold text example"
          defaultMessage="Look here, I can include HTML tags in plain string and render them as HTML: <b>Bold</b>, <i>Italics</i> and <a>links too</a>."
          values={{
            b: (chunks) => <b>{chunks}</b>,
            i: (chunks) => <i>{chunks}</i>,
            a: (chunks) => (
              <a class="external_link" target="_blank" href="https://jiga.dev/">
                {chunks}
              </a>
            )
          }}
        />
      </div>
    </IntlProvider>
  );
}

This should show an output as:

Play with code sample in this link: https://codesandbox.io/s/react-render-html-from-props-string-c9ogm

You can also pass the defaultMessage and values as prop values from other components.

In the above example, I have used the version 5 from the react-intl library. In the previous versions, the API is a little different. Follow the official documentation for more options such as using child elements.

Categories
PHP

Debug slow PHP applications using IntelliJ or PHPStorm

It is frustrating when your PHP application is really slow and you do not know which part of your code is taking up too much time.

Google considers 2 seconds as the optimal loading time for a fast loading website.

Enabling PHP XDebug Profiler

PHP’s Xdebug extension can be used to profile your web requests and CLI requests and understand why they are slow and which method is causing the bottleneck.

Follow this link to install XDebug on the server on which your PHP application is running: https://xdebug.org/docs/install

Once XDebug is installed successfully, open the xdebug configuration file and add the following line:

xdebug.profiler_enable = 1

Once you add the above line, the configuration file should look like this:

vagrant@dev:/etc/php/7.2/cli/conf.d$ cat 20-xdebug.ini
zend_extension=xdebug.so
xdebug.show_error_trace = 1
xdebug.profiler_enable = 1

The above file only enables the profile for CLI requests. For enabling the profile for web requests, add the same line into the following file:

vagrant@dev:/etc/php/7.2/fpm/conf.d$ cat 20-xdebug.ini
zend_extension=xdebug.so
xdebug.show_error_trace = 1
xdebug.profiler_enable = 1

Once the files are saved, restart the PHP FPM service:

service php7.2-fpm restart

Now you can open your phpinfo file and verify if the profiler is now enabled:

Generating the profile file

You can now open the slow webpage and PHP XDebug will create a profiler in the profiler_output_dir mentioned above (i.e. ./tmp) which can also be customized in the xdebug ini file.

I opened the homepage of my PHP application and found these files in my /tmp directory:

root@dev:/tmp# ls cache* -gGltrh
-rw-r--r-- 1 1.6K Sep 23 10:29 cachegrind.out.3315.01c00e
-rw-r--r-- 1 1.2M Sep 23 10:29 cachegrind.out.3315
-rw-r--r-- 1 2.1M Sep 23 10:31 cachegrind.out.3312
-rw-r--r-- 1 1.8K Sep 23 10:31 cachegrind.out.3312.0df7d7
-rw-r--r-- 1 1.2M Sep 23 10:31 cachegrind.out.3315.05bc8d
-rw-r--r-- 1 1.2M Sep 23 10:31 cachegrind.out.3312.073921
-rw-r--r-- 1 2.0K Sep 23 10:31 cachegrind.out.3312.03dc36

Analyzing the generated files

Once these files are generated, you can open them in IntelliJ or PHPStorm for analysis:

Once you have finished the analysis, make sure to remove or comment the line xdebug.profiler_enable in both your ini files and restart your FPM service. Otherwise the tmp directory will get filled very quickly.

Other Free Tools

Apart from paid tools such as IntelliJ/PHPStorm, there are some free tools to analyze the generated cachegrind files: