React ES6

When you’re starting out with React, you’ll often hear about something called ES6. So what is ES6, and why is it important in React development?

Let’s break it down simply.


What is ES6?

ES6 stands for ECMAScript 6, also known as ECMAScript 2015. It’s the 6th edition of the JavaScript language standard, and it brought many new features to make coding in JavaScript easier and more powerful.

React uses JavaScript, and modern React is written using ES6 features. So if you want to learn React, you must understand some basic ES6 concepts.


Why is ES6 important in React?

React code written today is mostly based on ES6. Without ES6, your React code would be long, repetitive, and harder to manage.

Here’s what ES6 helps with in React:

  • Makes components cleaner and shorter
  • Introduces modern ways to declare variables
  • Allows easier data handling with arrays and objects
  • Provides class-based components
  • Supports arrow functions and more readable syntax

ES6 Features Commonly Used in React

Below are the most important ES6 features used in React development, explained simply with examples:


1. Let and Const

In ES6, we use let and const instead of var.

  • const is used when the value should not change.
  • let is used when the value can change.
const name = "React";
// name = "Angular"; This will give error

let count = 0;
count = count + 1; // This is allowed

In React, you often use const to define components or states.


2. Arrow Functions

Arrow functions provide a shorter way to write functions.

// Traditional
function greet() {
return "Hello!";
}

// ES6 Arrow function
const greet = () => "Hello!";

In React, arrow functions are useful for writing cleaner component functions and event handlers.


3. Classes

ES6 introduced classes, which allow us to create components in a structured way.

class Hello extends React.Component {
render() {
return <h1>Hello World</h1>;
}
}

Although React Hooks are now preferred, class components are still used in many projects.


4. Destructuring

ES6 makes it easy to extract values from objects and arrays.

const person = { name: "Janhvika", age: 22 };

// Old way
const name = person.name;

// ES6 way
const { name, age } = person;

In React, destructuring is used in props and state:

function Welcome({ name }) {
return <h1>Welcome {name}</h1>;
}

5. Template Literals

Template literals allow you to embed variables inside strings using backticks:

const name = "React";
console.log(`Welcome to ${name}!`);

In React JSX:

<h2>{`Hello, ${userName}`}</h2>

6. Modules – Import and Export

ES6 introduced module system using import and export.

Export from one file:

export const name = "React";

Import in another file:

import { name } from "./App";

React apps use this to import components, styles, or data.


7. Spread and Rest Operators

Spread (...) is used to copy or merge arrays/objects.
Rest (...) collects multiple elements into one.

// Spread
const arr1 = [1, 2];
const arr2 = [...arr1, 3, 4]; // [1, 2, 3, 4]

// Rest
function sum(...numbers) {
return numbers.reduce((total, n) => total + n);
}

In React:

const newState = { ...prevState, updatedValue: 5 };

8. Array Functions – Map, Filter, Find

ES6 provides powerful functions for working with arrays.

const numbers = [1, 2, 3];
const doubled = numbers.map(n => n * 2); // [2, 4, 6]

In React, map() is often used to render lists:

const names = ["Janhvika", "Aman"];
return (
<ul>
{names.map(name => <li key={name}>{name}</li>)}
</ul>
);

Summary

ES6 FeatureUse in React
let & constDeclaring variables and constants
Arrow functionsWriting short functions & event handlers
ClassesCreating class components
DestructuringExtracting props and state
Template literalsInserting variables in JSX
Import/ExportModular coding with components
Spread/RestManaging state and props
Array methodsRendering dynamic lists

Final Words

Learning ES6 is essential for working with React efficiently. It makes your code shorter, cleaner, and easier to manage.

If you’re new to React, don’t try to learn all of JavaScript at once. Just start with these useful ES6 features, and you’ll be ready to build modern React apps.