Share
Explore

JavaScript Concepts to Know Before Learning React


If you want to learn React –or any JavaScript framework – you'll first need to understand the fundamental JavaScript methods and concepts.
Many developers choose a "learn as you go" approach when learning React. But this often doesn't result in productivity, and instead worsens the gaps in their JavaScript knowledge. This approach makes assimilating each new feature twice as difficult (you might begin to confuse JavaScript with React).
React is a JavaScript framework for building UI components-based user interfaces. All of its code is written in JavaScript, including the HTML markup, which is written in JSX (this enables developers to easily write HTML and JavaScript together).

Lab Learning Outcomes:

Go over all of the JS ideas and techniques you'll need to grasp before learning React.
React is built using modern JavaScript features, which were mostly introduced with ES2015.

The JavaScript You Need to Know Before Learning React

Callback Functions in JavaScript
A callback function is a function that is performed after another function has completed its execution. It is typically supplied as an input into another function.
Callbacks are critical to understand since they are used in array methods (such as map(), filter(), and so on), setTimeout(), event listeners (such as click, scroll, and so on), and many other places.
Here's an example of a "click" event listener with a callback function that will be run whenever the button is clicked:
//HTML
<button class="btn">Click Me</button>
//JavaScript
const btn = document.querySelector('.btn');
btn.addEventListener('click', () => {
let name = 'John doe';
console.log(name.toUpperCase())
})
NB: A callback function can be either an ordinary function or an arrow function.
Promises in JavaScript
As previously stated, a callback function is executed after the original function is executed. You may now begin to consider stacking so many callback functions on top of each other because you do not want a specific function to run until the parent function has finished running or a specific time has passed.

For example, let's attempt to display 5 names in the console after 2 seconds each – that is, the first name appears after 2 seconds, the second after 4 seconds, and so on...
setTimeout(() => {
console.log("Joel");
setTimeout(() => {
console.log("Victoria");
setTimeout(() => {
console.log("John");
setTimeout(() => {
console.log("Doe");
setTimeout(() => {
console.log("Sarah");
}, 2000);
}, 2000);
}, 2000);
}, 2000);
}, 2000);
This above example will work, but it will be difficult to comprehend, debug, or even add error handling to. This is referred to as"Callback Hell". Callback hell is a big issue caused by coding with complex nested callbacks.
The primary reason for using promises is to prevent callback hell. With Promises, we may write asynchronous code in a synchronous manner.
A promise is an object that returns a value that you anticipate to see in the future but do not now see.
A practical use for promises would be in HTTP requests, where you submit a request and do not receive a response right away because it's an asynchronous activity. You only receive the answer (data or error) when the server responds.

JavaScript promise syntax:

const myPromise = new Promise((resolve, reject) => {
// condition
});
Promises have two parameters, one for success (resolve) and one for failure (reject). Each has a condition that must be satisfied in order for the Promise to be resolved – otherwise, it will be rejected:
const promise = new Promise((resolve, reject) => {
let condition;
if(condition is met) {
resolve('Promise is resolved successfully.');
} else {
reject('Promise is rejected');
}
});
There are 3 states of the Promise object:
Pending: by default, this is the Initial State, before the Promise succeeds or fails.
Resolved: Completed Promise
Rejected: Failed Promise
Finally, let's try to re-implement the callback hell as a promise:
function addName (time, name){
return new Promise ((resolve, reject) => {
if(name){
setTimeout(()=>{
console.log(name)
resolve();
},time)
}else{
reject('No such name');
}
})
}
addName(2000, 'Joel')
.then(()=>addName(2000, 'Victoria'))
.then(()=>addName(2000, 'John'))
.then(()=>addName(2000, 'Doe'))
.then(()=>addName(2000, 'Sarah'))
.catch((err)=>console.log(err))

Map() in JavaScript

One of the most often used methods is Array.map(), which allows you to iterate over an array and modify its elements using a callback function. The callback function will be run on each array element.
Assume we have an array of users that contains their information.
let users = [
{ firstName: "Susan", lastName: "Steward", age: 14, hobby: "Singing" },
{ firstName: "Daniel", lastName: "Longbottom", age: 16, hobby: "Football" },
{ firstName: "Jacob", lastName: "Black", age: 15, hobby: "Singing" }
];
We can loop through using map and modify it’s output
let singleUser = users.map((user)=>{
//let's add the firstname and lastname together
let fullName = user.firstName + ' ' + user.lastName;
return `
<h3 class='name'>${fullName}</h3>
<p class="age">${user.age}</p>
`
});
You should note that:
map() always returns a new array, even if it’s an empty array.
It doesn’t change the size of the original array compared to the filter method
It always makes use of the values from your original array when making a new one.
Gotcha: The map method works almost like every other JavaScript iterator such as forEach() but it’s proper to always use the map method whenever you are going to return a value.
Here is a perfect description by
One of the key reasons we use map is so we can encapsulate our data in some HTML, whereas for React this is simply done using JSX.
You can read more about map() by searching on MSDN.
Filter() and Find() in JavaScript
Filter() provides a new array depending on certain criteria. Unlike map(), it can alter the size of the new array, whereas find() returns just a single instance (this might be an object or item). If several matches exist, it returns the first match – otherwise, it returns undefined.
Suppose you have an array collection of registered users with different ages:
let users = [
{ firstName: "Susan", age: 14 },
{ firstName: "Daniel", age: 16 },
{ firstName: "Bruno", age: 56 },
{ firstName: "Jacob", age: 15 },
{ firstName: "Sam", age: 64 },
{ firstName: "Dave", age: 56 },
{ firstName: "Neils", age: 65 }
];
You could choose to sort this data by age groups, such as young individuals (ages 1-15), senior people (ages 50-70), and so on...
In this case, the filter function comes in handy as it produces a new array based on the criteria. Let's have a look at how it works.



// for young people
const youngPeople = users.filter((person) => {
return person.age <= 15;
});
//for senior people
const seniorPeople = users.filter((person) => person.age >= 50);
console.log(seniorPeople);
console.log(youngPeople);
This generates a new array. It produces an empty array if the condition is not satisfied(no match).
Find()
The find() method, like the filter() method, iterates across the array looking for an instance/item that meets the specified condition. Once it finds it, it returns that specific array item and immediately terminates the loop. If no match is discovered, the function returns undefined.
For example:
const Bruno = users.find((person) => person.firstName === "Bruno");
console.log(Bruno);
Destructuring Arrays and Objects in JavaScript
Destructuring is a JavaScript feature introduced in ES6 that allows for faster and simpler access to and unpacking of variables from arrays and objects.
Before destructuring was introduced, if we had an array of fruits and wanted to get the first, second, and third fruits separately, we would end up with something like this:
let fruits= ["Mango", "Pineapple" , "Orange", "Lemon", "Apple"];
let fruit1 = fruits[0];
let fruit2 = fruits[1];
let fruit3 = fruits[2];
console.log(fruit1, fruit2, fruit3); //"Mango" "Pineapple" "Orange"
This is like repeating the same thing over and over which could become cumbersome. Let's see how this could be distructured to get the first 3 fruits.
let [fruit1, fruit2, fruit3] = fruits;
console.log(fruit1, fruit2, fruit3); //"Mango" "Pineapple" "Orange"
You might be wondering how you could skip data if you just want to print the first and final fruits, or the second and fourth fruits. You would use commas as follows:
const [fruit1 ,,,, fruit5] = fruits;
const [,fruit2 ,, fruit4,] = fruits;
Object destructuring
Let’s now see how we could destructure an object – because in React you will be doing a lot of object descructuring.
Suppose we have an object of user which contains their firstname, lastname, and lots more,
const Susan = {
firstName: "Susan",
lastName: "Steward",
age: 14,
hobbies: {
hobby1: "singing",
hobby2: "dancing"
}
};
In the old way, getting these data could be stressful and full of repetition:
const firstName = Susan.firstName;
const age = Susan.age;
const hobby1 = Susan.hobbies.hobby1;
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
but with destructuring its a lot easier:
const {firstName, age, hobbies:{hobby1}} = Susan;
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
We can also do this within a function:
function individualData({firstName, age, hobbies:{hobby1}}){
console.log(firstName, age, hobby1); //"Susan" 14 "singing"
}
individualData(Susan);
Rest and Spread Operators in JavaScript
JavaScript spread and rest operators use three dots .... The rest operator gathers or collects items – it puts the “rest” of some specific user-supplied values into a JavaScript array/object.
Suppose you have an array of fruits:
let fruits= ["Mango", "Pineapple" , "Orange", "Lemon", "Apple"];
We could destructure to get the first and second fruits and then place the“rest” of the fruits in an array by making use of the rest operator.
const [firstFruit, secondFruit, ...rest] = fruits
console.log(firstFruit, secondFruit, rest); //"Mango" "Pineapple" ["Orange","Lemon","Apple"]
Looking at the result, you'll see the first two items and then the third item is an array consisting of the remaining fruits that we didn't destructure. We can now conduct any type of processing on the newly generated array, such as:
const chosenFruit = rest.find((fruit) => fruit === "Apple");
console.log(`This is an ${chosenFruit}`); //"This is an Apple"
It's important to bear in mind that this has to come last always (placement is very important).
We've just worked with arrays – now let's deal with objects, which are absolutely the same.
Assume we had a user object that has their firstname, lastname, and a lot more. We could destructure it and then extract the remainder of the data.
const Susan = {
firstName: "Susan",
lastName: "Steward",
age: 14,
hobbies: {
hobby1: "singing",
hobby2: "dancing"
}
};
const {age, ...rest} = Susan;
console.log(age, rest);
This will log the following result:
14
{
firstName: "Susan" ,
lastName: "Steward" ,
hobbies: {...}
}
Let’s now understand how the spread operator works, and finally summarize by differentiating between both operators.
Spread operator
The spread operator, as the name implies, is used to spread out array items. It gives us the ability to get a list of parameters from an array. The spread operator has a similar syntax to the rest operator, except it operates in the opposite direction.
Note: A spread operator is effective only when used within array literals, function calls, or initialized properties objects.
For example, suppose you have arrays of different types of animals:
let pets= ["cat", "dog" , "rabbits"];
let carnivorous = ["lion", "wolf", "leopard", "tiger"];
You might want to combine these two arrays into just one animal array. Let's try it out:
let animals = [pets, carnivorous];
console.log(animals); //[["cat", "dog" , "rabbits"], ["lion", "wolf", "leopard", "tiger"]]
This is not what we want – we want all the items in just one single array. And we can achieve this using the spread operator:
let animals = [...pets, ...carnivorous];
console.log(animals); //["cat", "dog" , "rabbits", "lion", "wolf", "leopard", "tiger"]
This also works with objects. It is important to note that the spread operator cannot expand the values of object literals, since a properties object is not an iterable. But we can use it to clone properties from one object into another.
For example:
let name = {firstName:"John", lastName:"Doe"};
let hobbies = { hobby1: "singing", hobby2: "dancing" }
let myInfo = {...name, ...hobbies};
console.log(myInfo); //{firstName:"John", lastName:"Doe", hobby1: "singing", hobby2: "dancing"}
You can read more on JavaScript Spread and Rest operators
.
Unique Value - Set() in JavaScript
Recently, I was try to create a categories tab for an application where I needed to fetch the categories value from an array.
let animals = [
{
name:'Lion',
category: 'carnivore'
},
{
name:'dog',
category:'pet'
},
{
name:'cat',
category:'pet'
},
{
name:'wolf',
category:'carnivore'
}
]
The first thing was to loop through the array, but I got repeated values:
let category = animals.map((animal)=>animal.category);
console.log(category); //["carnivore" , "pet" , "pet" , "carnivore"]
This meant that I needed to set up a condition to avoid repetition. It was a little bit tricky until I came across the set() constructor/object provided by ES6 :).
A set is a collection of items which are unique, that is no element can be repeated. Let’s see how we can implement this easily.
//wrap your iteration in the set method like this
let category = [...new Set(animals.map((animal)=>animal.category))];
console.log(category); ////["carnivore" , "pet"]
NB: I decided to spread the values into an array. You can read more on unique values
.
Dynamic Object keys in JavaScript
This enables us to add object keys using square bracket notation. This may not make sense to you right now, but as you continue learning React or begin working with teams, you may come across it.
In JavaScript, we know that objects are often made up of properties/keys and values, and we may use the dot notation to add, edit, or access some value(s). As an example:
let lion = {
category: "carnivore"
};
console.log(lion); // { category: "carnivore" }
lion.baby = 'cub';
console.log(lion.category); // carnivore
console.log(lion); // { category: "carnivore" , baby: "cub" }
We also have the option of using square bracket notation, which is utilized when we need dynamic object keys.
What do we mean by dynamic object keys? These are keys that might not follow the standard naming convention of properties/keys in a object. The only permits camelCase and snake_case, but by using square bracket notation we can solve this problem.
For example, suppose we name our key with a dash in between words, for example (lion-baby):
let lion = {
'lion-baby' : "cub"
};
// dot notation
console.log(lion.lion-baby); // error: ReferenceError: baby is not defined
// bracket notation
console.log(lion['lion-baby']); // "cub"
You can see the difference between the dot notation and the bracket notation. Let's see other examples:
let category = 'carnivore';
let lion = {
'lion-baby' : "cub",
[category] : true,
};
console.log(lion); // { lion-baby: "cub" , carnivore: true }
You can also perform more complex operations by using conditions within the square bracket, like this:
const number = 5;
const gavebirth = true;
let animal = {
name: 'lion',
age: 6,
[gavebirth && 'babies']: number
};
console.log(animal); // { name: "lion" , age: 6 , babies: 5 }
You can read more about this
.
reduce() in JavaScript
This is arguably the most powerful array function. It can replace the filter() and find() methods and is also quite handy when doing map() and filter() methods on large amounts of data.
When you chain map and filter method together, you wind up doing the work twice – first filtering every single value and then mapping the remaining values. On the other hand, reduce() allows you to filter and map in a single pass. This method is powerful, but it's also a little more sophisticated and tricky.
We iterate over our array and then obtain a callback function, which is similar to map(), filter(), find(), and the others. The main distinction is that it reduces our array to a single value, which may be a number, array, or object.
Another thing to keep in mind about the reduce() method is that we are passing in two arguments, which has not been the case since you began reading this tutorial.
The first argument is the sum/total of all computations, and the second is the current iteration value (which you will understand shortly).
For example, suppose we have a list of salaries for our staff:
let staffs = [
{ name: "Susan", age: 14, salary: 100 },
{ name: "Daniel", age: 16, salary: 120 },
{ name: "Bruno", age: 56, salary: 400 },
{ name: "Jacob", age: 15, salary: 110 },
{ name: "Sam", age: 64, salary: 500 },
{ name: "Dave", age: 56, salary: 380 },
{ name: "Neils", age: 65, salary: 540 }
];
And we want to calculate a 10% tithe for all staff. We could easily do this with the reduce method, but before doing that let's do something easier: let’s calculate total salary first.
const totalSalary = staffs.reduce((total, staff) => {
total += staff.salary;
return total;
},0)
console.log(totalSalary); // 2150
NB: We passed a second argument which is the total, it could be anything – for example a number or object.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.