Simple local development environment for web apps

  1. Open your app working directory
    • There you should have at least index.html
  2. Launch http-server in your terminal
    • If you haven’t installed it yet: npm install http-server -g
  3. Launch chromium-browser --disable-web-security --user-data-dir=/tmp/something in your terminal to allow all origins (temporary ignore  CORS mechanism )
    • Make sure you don’t have other instances open: pkill -f chromium
  4. Type in the address bar one of the address from your http-server info to access to your app

Note: Use Chromium without security just with your own code for testing and development, do not browse with it and beware about using third-party software in your code.

Create an universal React app

First of all read the whole post, secondly install Create React App globally and create an app using create-react-app my-app, then before any coding add packages to your app. As soon as you finish all package installations you can start coding using my basics code snippets if you need to 🙂

Add main React package to your enviroment

Create React App

npm i -g create-react-app

This will allow you to create React Apps with no build configuration anywhere in your computer.

create-react-app my-app

cd my-app/

npm start

This will create a React App called my-app pointing to http://localhost:3000/, any time you save your files your app will be updated automatically.

Link to the library.

Add packages to your React app

Within my-app folder.

Bootstrap

npm i -S bootstrap

npm i -S react-bootstrap

npm i -S react-router-bootstrap

This will allow us to use this front-end framework in a easier way (with React).

Links to the library.

For example lets code our home page (home.js)

import React from 'react';
import { Grid, Row, Col, Jumbotron, Image } from 'react-bootstrap';

import people from '../media/people.png';

function Home(props) {
  const t = props.t; // we will pass the translation function via props
  return (
    <div>
      <Grid>
        <Jumbotron>
          <h1>{t('Open knowledges')}</h1>
          <p>Cool text right here</p>
        </Jumbotron>
      </Grid>

      <Grid>
        <Row>
          <Col lg={12}>
            <p>I love paragraphs.</p>
            <p>I love paragraphs.</p>
          </Col>
        </Row>
        <Row>
          <Col lg={12}>
            <p>I love paragraphs.</p>
            <Image className="detached" src={people} responsive />
          </Col>
        </Row>
      </Grid>
    </div>
  );
}

export default Home;

Anything that you would use repeatedly in other pages of your website should be in a different file (component), such as the navigation bar and the footer. Create a layout component to organize your components all together (layout.js)

import React, { Component } from 'react';

import Navigation from '../components/Navigation';
import Footer from '../components/Footer';

class Layout extends Component {
  render() {
    return (
      <div>
        <Navigation t={this.props.t}></Navigation>
        {this.props.children}
        <Footer></Footer>
      </div>
    );
  }
}

export default Layout;

React Router

npm i -S react-router

npm i -S react-router-dom

This will make our website’s routing easier.

Link to the library.

Let’s create our website/app (index.js) with two pages, for example Home (home.js) and About (about.js)

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route } from 'react-router-dom';

import './index.css';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';

import Layout from './pages/Layout';
import Home from './pages/Home';
import About from './pages/About';
import i18n from './i18n';

let t = i18n.t.bind(i18n); // for translations

ReactDOM.render(
  <BrowserRouter>
    <div>
      <Route exact path="/" render={() => <Layout t={t}><Home t={t} /></Layout>} />
      <Route exact path="/about" render={() => <Layout t={t}><About t={t} /></Layout>} />
    </div>
  </BrowserRouter>,
  document.getElementById('root')
);

Add i18next package

npm i -S i18next

This will let us do our website/app internationalization.

Link to the library.

import i18n from 'i18next';

i18n
  .init({
    fallbackLng: 'en',
    lng: navigator.language.split('-')[0],
    debug: true,
    resources: {
      en: {
        translation: {
          "Hello": "Hola",
          "Bye": "Adiós"
        }
      }
    }
  });

export default i18n;

Futher details using i18n in this post: Create a multilingual React website.

How your files structure would look like?

.
+-- _components
|   +-- Footer.css
|   +-- Footer.js
|   +-- Navigation.css
|   +-- Navigation.js
+-- _media
|   +-- logo.svg
+-- _pages
|   +-- Layout.js
|   +-- Home.js
|   +-- About.js
+-- i18n.js
+-- index.css
+-- index.js

 

My Bash Prompt customization

14:34 gengns@mycomputer  Desktop$ ▮

Open a console in your HOME folder and type vim .bashrc, then copy the next line at the end of the file:

PS1='\[\e[30;47m\]\A \u@\h \W\[\e[0m\]$ '
  • \[\e[30;47m\] # 30 is black foreground color and 47 is light grey background
  • \A # hour in hh:mm format
  • \u # user name
  • \h # host name
  • \W  # working folder name
  • \[\e[0m\] # reset

More info: customization and colors in Bash

Basic React example on JSFiddle

Basic React example to synchronize (write) text in an HTML heading tag while typing in an input tag.

Do not forget to set Babel on JSFiddle and add react.js and react-dom.js libraries.

Xamarin is not a good candidate for phone apps

A few days ago an acquaintance pass me an article about Xamarin as the new best candidate for phone apps. Sorry, but I’ve never laughed more in my life. He compered it with Phonegap (Cordova) telling me that it’s more powerfull… but the real thing is that it’s not and I’m going to tell you just some couple of good reasons.

  1. Xamarin only supports iOS, Android and Windows phone plataforms whereas Cordova supports iOs, Android, Windows, Amazon Fire OS, Backberry, Ubuntu, Firefox OS, LG webOS, etc.
  2. You have to pay to use Xamarin and it’s not Opensource whereas Cordova is Opensource and free.
  3. Xamarin doesn’t support Web Standards whereas Cordova supports Web Standards.
  4. Both have access to the device API.
  5. Xamarin uses it’s own native interface whereas Cordova uses Web UI which is the most widespread.
  6. Neither of both have a full native performance for all the platforms.
  7. Xamarin uses C# whereas Cordova uses HTML5, CSS and JavaScript.

However, the real comparison would be with Qt which uses C++ (a compiled language) but even Qt is better, more mature, more tested, opensource and free, and with even more supported platforms.

So, if you want to lose your time and make things go slower, feel free to use Xamarin. I think at the end some people are as blind with Technologies and Brands as they are with Politics and Religions. You can’t argue with them because they don’t like the proofs and the truth (they feel like you were attacking their physical integrity) or maybe you are a C# programmer who doesn’t want to learn new technologies in your life (more reasonable).

And why not use Qt? Well, I’ve been developing with Qt since 2007, when I was at the University (before tablets, phones and wearables boom). Qt is really heavy, complex, with a steep learning curve if you want to create awesome interfaces. One of the best things was its access to the device API (cross platform) but Web Technologies have caught it up.

We advance to make easier, open and powerful apps. I know, it’s hard to forget what you’ve learned but you have to deal with it because at the end you will thank yourself.