Giter Site home page Giter Site logo

react-babel-plugins-v-000's Introduction

React Babel Plugins

Overview

We'll explain what Babel does and how to use it for React development.

Objectives

  1. Define and describe the benefits of using Babel plugins
  2. Find Babel plugins and install them
  3. Describe a few useful plugins for React development

Babel recap

Babel is used to transform our ES2015 (and even newer) code to ES5 โ€” the previous version of JavaScript that all browsers know and understand. Most of the ES2015 features are already present in browsers, but it's best to transpile your code using Babel anyway.

This ensures that every browser can run your code, as well as giving you the possibility of writing even more modern code (using features that haven't been released yet). Babel, installed by itself, does nothing to your code. It only starts transforming your code once you tell it which plugins to use.

Plugins?

Plugins are small, composable dependencies that transform parts of our code. These plugins get applied to the code when compiling it with Babel, each doing their own little job and changing our code. For example, the transform-es2015-destructuring allows us to use ES2015 destructuring in our code:

// Source code
const { foo, bar } = myLib;

// Gets transformed by the plugin to:
var _myLib = myLib;
var foo = _myLib.foo;
var bar = _myLib.bar;

Having small, separate plugins like this allows us to tweak our configuration to our heart's desire. However, installing every single plugin just to write ES2015 and React code seems like such a hassle... Luckily, there's a thing in Babel called plugin presets! These dependencies are basically a collection of plugins that are grouped together. For example, to transform the code we're writing in this course, we use babel-preset-es2015 and babel-preset-react. Of course, if we want to add additional plugins, we can do so without any restriction!

Using plugins and presets

Now that we know how plugins and presets work, let's take a look at how tell Babel to actually use them. We install them using npm, and then we use a file called .babelrc in the root of our project to configure Babel:

{
  "presets": ["es2015", "react"],
  "plugins": ["an-example-plugin", "another-example-plugin"]
}

Now when Babel compiles our code, it'll use the presets and plugins we've defined above. Babel has a great list of all available plugins that you can use to see if you'd like to add anything else.

Notable plugins

While the following plugins are at an experimental stage, they're still worth checking out โ€” they make the development of React applications even easier!

Object rest & spread

Using the babel-plugin-transform-object-rest-spread plugin, we can use the spread operator for objects, much like you can already do in ES2015 with arrays. An example from the docs:

// Rest properties
let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }

// Spread properties
let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }

Class properties

Using the babel-plugin-transform-class-properties plugin, we can use class properties to declare our methods, alleviating the need to use .bind() in the constructor.

Resources

react-babel-plugins-v-000's People

Contributors

annjohn avatar thomastuts avatar pletcher avatar

Watchers

James Cloos avatar Bill Story avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.