1 of 49

JavaScript

prerequisites

for React (hooks)

Reactjsday 2020

Jad Joubran

Instructor & Consultant

@JoubranJad

2 of 49

JS <> React

React, like any frontend framework, builds on top of JS concepts

It’s important to be able to recognize those JS concepts

This talk covers React (hooks), thus no classes

3 of 49

JS Concepts (React usage)

  1. Automatic Semicolon Insertion (returning JSX)
  2. Object destructuring (props)
  3. Array destructuring (useState hook)
  4. Immutability (state update)
  5. Closures (state & events)

4 of 49

1. Automatic

Semicolon Insertion

(ASI)

5 of 49

Semicolons are optional

In JavaScript

6 of 49

Scenarios for ASI

let, const�import, export

continue, break, throw

return

7 of 49

function getAgeLimit() {

let age = 18

return

age

}

8 of 49

function getAgeLimit() {

let age = 18

return;

age

}

9 of 49

function getAgeLimit() {

let age = 18

return age

}

10 of 49

function getAgeLimit() {

let age = 18

return (

age

)

}

11 of 49

import React from "react";

function App() {

return

<main>

<h1>Hello World!</h1>

</main>

}

12 of 49

import React from "react";

function App() {

return;

<main>

<h1>Hello World!</h1>

</main>

}

13 of 49

import React from "react";

function App() {

return (

<main>

<h1>Hello World!</h1>

</main>

)

}

14 of 49

import React from "react";

function Navbar() {

return (<>

<nav>Home</nav>

</>);

}

15 of 49

2. Object

Destructuring

16 of 49

const point = {

lat: 52.369661,

lng: 4.897243

};

const lat = point.lat;

const lng = point.lng;

17 of 49

const point = {

lat: 52.369661,

lng: 4.897243

};

// const lat = point.lat;

// const lng = point.lng;

const {lat, lng} = point;

18 of 49

const point = {

lat: 52.369661,

lng: 4.897243

};

// const lat = point.lat;

// const lng = point.lng;

const {lat, lng, elevation = 0} = point;

19 of 49

import React from "react";

function App(props) {

console.log(props); // {}

return null;

}

// Usage

<App />

20 of 49

import React from "react";

function App(props) {

console.log(props); // {title: "My App"}

return <div>{props.title}</div>;

}

// Usage

<App title="My App"/>

21 of 49

import React from "react";

function App(props) {

const {title} = props;

return <div>{title}</div>;

}

// Usage

<App title="My App"/>

22 of 49

import React from "react";

function App({title}) {

return <div>{title}</div>;

}

// Usage

<App title="My App"/>

23 of 49

import React from "react";

function Button(props) {

return (<button className={props.className} disabled={props.disabled}>{props.children}

</button>);

}

// Usage

<Button disabled>Login</Button>

24 of 49

import React from "react";

function Button(props) {

const {children, ...rest} = props;

return (

<button {...rest}>

{children}

</button>

);

}

25 of 49

3. Array

Destructuring

26 of 49

const coordinates = [52.36, 4.89];

const lat = coordinates[0];

const lng = coordinates[1];

console.log(lat); // 52.36

27 of 49

const coordinates = [52.36, 4.89];

// const lat = coordinates[0];

// const lng = coordinates[1];

const [lat, lng] = coordinates;

console.log(lat); // 52.36

28 of 49

function getCoordinates() {

return [52.36, 4.89];

}

const [lat, lng] = getCoordinates();

console.log(lat); // 52.36

29 of 49

function useState() {

const currentValue = 10;

function updateFn(){

console.log("update state");

}

return [currentValue, updateFn];

}

const [count, setCount] = useState();

console.log(count, setCount); // 10, function

30 of 49

import React, {useState} from "react";

function Stopwatch() {

const [seconds, setSeconds] = useState(0);

return <div>{seconds}</div>

}

31 of 49

import React, {useState} from "react";

function Button() {

const [disabled, setDisabled] = useState(false);

return <button disabled={disabled}/>

}

32 of 49

import React, {useState} from "react";

function Button() {

const [disabled, setDisabled] = useState(false);

function handleClick() {

setDisabled(!disabled);

}

return <button disabled={disabled} onClick={handleClick}/>

}

33 of 49

4. Immutability

34 of 49

15 === 15; //true

"hello" === "hello"; //true

true === true; //true

// however

[] === []; //false

{} === {}; //false

[10] === [10]; //false

35 of 49

new Array(); // creates []

new Object(); //creates {}

// now this starts making sense

new Array() === new Array(); //false

new Object() === new Object(); //false

36 of 49

import React, { useState } from "react";

function App() {

const [data, setData] = useState([]);

function handleClick() {

// ❌ incorrect way

data.push(10);

setData(data);

}

return <button onClick={handleClick}>Add 10</button>;

}

37 of 49

let state = []; //created by useState([])

//newState is the result of data.push(10) on button click

function setData(newState) {

if (state === newState) {

//no need to re-render because the state has not changed

return false;

}

//store the newState for next time the setData() is called

state = newState;

//ask ReactDOM to re-render

}

38 of 49

state === newState // true, React won't re-render whereas the state has been changed

// We need to update the array immutably

// => Create a new copy of that array

39 of 49

// instead of .push()

const numbers = [1, 2, 3];

const result = [...numbers, 4];

console.log(result); //[1, 2 ,3 ,4]

40 of 49

import React, { useState } from "react";

function App() {

const [data, setData] = useState([]);

function handleClick() {

// ✅ correct way

setData([...data, 10]);

}

return <button onClick={handleClick}>Add 10</button>;

}

41 of 49

5. Closures

42 of 49

What is a closure?

A closure is when a function contains another function.

Functions in JS have access to their outer scope.

This concept is called closures.

43 of 49

What is a closure? (continued)

Closures is what you get when you define a function, it's not something you have to "enable" or decide to use.

Functions defined inside another function can use the variables defined in the outer function.

44 of 49

import React, {useState} from "React";

function Stopwatch() {

const [seconds, setSeconds] = useState(0);

function handleClick() {

// (prefer functional state updates over this)

setSeconds(seconds + 1);

}

return <button onClick={handleClick}>Add 10</button>;

}

45 of 49

6. Conclusion

46 of 49

In my opinion

It’s better to understand the JavaScript concepts before diving into a framework.

As long as don’t get stuck trying to understand those concepts.

47 of 49

Alternatively

Take a step back when you encounter JS concepts and learn them outside of the framework you’re using

48 of 49

Examples are taken from react-tutorial.app

49 of 49

Thank you!

Get the slides bit.ly/js-pre-react

Learn React interactively react-tutorial.app

Reactjsday 2020

Jad Joubran

Instructor & Consultant

@JoubranJad