What is a component?

a reusable, self-contained code that contains rendering logic and JSX

Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

React docs

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/704faaf0-e705-430b-a4e6-5adf295fb747/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b207e9f2-eef1-4135-9be3-030fa05180be/Untitled.png

Components are standardized, interchangeable building blocks of UIs.

They encapsulate the appearance and function of UI pieces. Think LEGO bricks. LEGOs can be used to build everything from castles to spaceships; components can be taken apart and used to create new features.

Component driven UI

🏘 Case study: Airbnb and UberEats

http://bit.ly/2TqjYa1

https://www.ubereats.com/en-CA/

Can you spot the "components" in these page?

🏷 Sidenote: React dev tools extension

Install React dev tools for your browser (available in all major browsers)

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/408a9cad-7e74-4478-9481-1f109962081b/Untitled.png

😎 You can inspect React component tree of some React websites

Airbnb: http://bit.ly/2TqjYa1

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9e98491b-159e-48e8-b41c-62eaf11cfba5/Untitled.png

Ubereats: https://www.ubereats.com/en-CA/

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f5b9344a-47e4-40db-88d5-d839d3a284af/Untitled.png

🏷 ES6 Arrow functions

Throughout the workshop, you'll see ES6 syntax which is a new standard of JS

Arrow functions: () => {}

The syntax comes off as weird if you're seeing it for the first time, but these are all equivalent.

These are named functions

// ES5 version
function add(a, b) {
  return a + b;
}
// or
var add = function(a, b) {
	return a + b;
}

// ES6 version
const add = (a, b) => {
  return a + b;
};

🏷 ES6 Side note: Arrow function shorthand

Consider this anonymous (unnamed) function:

// ES5
function(item) {
	return item*2;
}

// ES6 full syntax
(item) => {
	return item*2;
}

// ES6 shorthand 1: return and {} optional if only one expression
// perfect for one-liners
(item) => item*2

// ES6 shorthand 2: () optional when only one param
item => item*2