Week 3 Basics-2
Week 3 Basics-2
React
Basics-2
CENG 475
2
JavaScript
In case it’s been some time since you last worked with JavaScript
Core Syntax & Rules Essential,
Modern JavaScript Features
Key JavaScript Features Used In React Apps
CENG 475
3 JavaScript Can Be Executed In
Many Environments
JavaScript code can be Thanks to Node.js or Deno, With extra technologies like
included in any website JavaScript code can be Capacitor or React Native,
The code then executes executed outside of the you can build mobile apps
inside the browser (i.e., on browser, too based on JavaScript
the machine of the website The code then executes The code then executes on
visitor) directly on the machine the mobile device
CENG 475
4 Adding JavaScript Code To A Website
<script>
<script src=“script.js”>
alert('Hello')
</script>
</script>
CENG 475
5 JavaScript Code Consists Of
“Statements”
Statement
Values /
Keywords Identifiers
Expressions
Built into JavaScript Defined by Defined by developers
let, if, for, … developers Used to Hardcoded values or
Required to “tell” identify commands expressions that
JavaScript that a (functions), produce new values
certain feature is variables (values)
used etc. ‘Hello world’, 5 - 3, …
alert(), age, …
CENG 475 let, if, for, …
6
Keywords
Keywords enable language features
CENG 475
7
Identifiers
• Variables,
• Functions (“Commands”)
• Parameters
• Property
CENG 475
8
React Projects
CENG 475
"name": "week2",
"version": "0.1.0",
"private": true,
React Projects
"dependencies": {
9 "@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
package.json "scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
lists all the dependencies "test": "react-scripts test",
"eject": "react-scripts eject"
},
of the code "eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
CENG 475 } }
10
React Projects
1. Raw, unprocessed React code won’t execute in the
browser
JSX
JSX is not a default JavaScript feature
CENG 475
12 React ES6
What is ES6?
ES6 stands for ECMAScript 6.
ECMAScript was created to standardize JavaScript, and ES6 is
the 6th version of ECMAScript, it was published in 2015, and is
also known as ECMAScript 2015.
CENG 475
13 Why Should I Learn ES6?
React uses ES6, and you should be familiar with some of
the new features like:
❑ Classes
❑ Arrow Functions
❑ Variables (let, const, var)
❑ Array Methods like .map()
❑ Destructuring
❑ Modules
❑ Ternary Operator
❑ Spread Operator
CENG 475
14 How to Use ES6 Modules?
Modules help you split various functionalities of your app
into separate files/scripts. You can have different scripts
for form validation, logging a user in, and so on.
CENG 475
16 Step 2 – Create and export functions
into separate files
Here's the function for addition in “src="assets/scripts/app.js"
CENG 475
17 Step 3 – Import the functions into
script.js
app.js;
util.js;
export default "adnasdoasflak1";
export let apiKey = "adnasdoasflak1";
export let abc = "abc";
CENG 475
18
Import & export
Since there is no build process, we need a web server working
at some place, locally perhaps
CENG 475
19
Import & export
1st solution with
VS Code
Extensions
Extensions
To stop;
Alt L + Alt C,
To restart server;
Alt L + Alt C
To stop or Exit:
CTRL+C
CENG 475
22
Import & export
Check console
CENG 475
23
Import & export
<head>
<title>JavaScript Refresher</title>
<link rel="stylesheet" href="assets/styles/main.css" />
<meta charset="UTF-8" />
<script src="assets/scripts/app.js" type="module"></script>
</head>
app.js;
import { apiKey } from "./util.js";
Pay attention
Your have to write with full name: *.js
CENG 475
24
ES6 Modules
Export is ES6's feature which is used to Export a module(file) and
use it in some other module(file).
Default Export:
1.default export is the convention if you want to export only
one object(variable, function, class) from the file(module).
2.There could be only one default export per file, but not
restricted to only one export(Named export).
3.When importing default exported object we can rename it as
well.
CENG 475
25
ES6 Modules
2. Export or Named Export:
CENG 475
26 Exports & Imports
There are two different types of exports: default (unnamed) and
namedexports
CENG 475
28 React Fundamentals
Instead:
import * as util from "./util.js";
Where util is an object, name is up to you.
CENG 475
Additionally Objects
31
Variables
Variables store Values
Variable
CENG 475
32
Why Use Variables?
1 2
Reusability Readability
CENG 475
33 Identifiers Must Follow Certain Rules
& Recommendations
1. Must not contain whitespace or special characters (except $ and _)
Valid: $userName, age, user_name, data$, …
Invalid: %userName, age/, user name, …
CENG 475
35
Variables vs Constants
Variables Constants
Allowed Error
CENG 475
36
Variables vs Constants
Variables Constants
console.log(userMessage);
console.log(userMessage+' CENG 475“’);
//code_03_13-18
CENG 475
37
Variables vs Constants
Values can be hardcoded
But they can also be derived via
Expressions & Operators
//console.log(userMessage);
console.log(userMessage+" CENG 475");
console.log(10 =='10’) //type equivalence
CENG 475
39 React Fundamentals
CENG 475
41 ES6 Arrow Functions
Anonymous functions are without names
function() {
// Function Body
}
OR,
Using function keyword or
( () => {
Arrow function notation
// Function Body...
} )();
OR,
CENG 475
42 ES6 Arrow Functions
var greet = () => {
console.log("Welcome to CENG 475! \nIs it cool?");
};
//code_03_21-41
greet();
OR,
Using function keyword or
( () => {
Arrow function notation
// Function Body...
} )();
OR,
CENG 475
44 ES6 Arrow Functions
Then working with Arrow Functions, you have a couple
of "syntax shortcuts" available.
Most importantly, you should know about the following
alternatives:
1) Omitting parameter list parentheses
If your arrow functions takes exactly one parameter, you
may omit the wrapping parentheses.
CENG 475
45 ES6 Arrow Functions
function callMe(name) {
console.log(name);
}
CENG 475
46 ES6 Arrow Functions
Ex 1:
function createGreeting(userName, message = "Hello!") {
console.log(userName);
console.log(message);
return "Hi, I am " + userName + ". " + message;
}
Ex 2:
function createGreeting(userName, message = "Hello!") {
return "Hi, I am " + userName + ". " + message;
}
const greeting1 = createGreeting("Ender ");
console.log(greeting1);
CENG 475
47 ES6 Arrow Functions ( ()=> )
The function is then stored in the greet variable. We can call the function by
invoking greet().
greet();
A self-executing function.
(function () {
console.log("Welcome to CENG 475!");
})();
//code_03_44-54
CENG 475
50 Arrays
Ex 3: //code_03_44-54
Ex 4:
findIndex method needs a function
const index1 = hobbies.findIndex("a");
const index = hobbies.findIndex((item) =>
{ return item == "32.04" });
CENG 475
const editedHobbies1 = hobbies.map((item) => (" ! "+item));
52 Arrays
Array.Map function is used for transforming any array item to any
other item.
Let’s do not change strings, and create JS objects by {}
We have to warp it with (), tell JS {} do not define the function body
Object
//code_3_74-80
CENG 475
54 Arrays
//code_3_83-90
Exam Question: Array Methods
Write a transformToObjects() function that should transform a list of
numbers into a list of JavaScript objects.
In the newly returned array, every object must have a val key and
the input array's number as a value.
CENG 475
55 Array map() function
Go to week2 src_209
A small example:
//code_209_03-17
CENG 475
56 Array map() function
const shoppingList = ['Oranges', 'Cassava', 'Garri', 'Ewa', 'Dodo',
'Books'];
export default function a(){
return (
shoppingList.map((item, index) => {
return (
<ol>
<li key={index}>{item}</li>
</ol>
)
})
)
}
//code_209_03-17
CENG 475
57 Array map() function
//code_209_23-33
CENG 475
58 Array map() function
const array = [
{
name: "Tim",
age: 27
},
{
name: "Bob",
age: 32
}
];
export default function App() {
return (
<div>
{array.map((item, index) => (
<div key={index}>
<div>Name: {item.name}</div>
<div>Age: {item.age}</div>
</div>
))} //code_209_23-48
</div>
)
CENG 475 }
59 Array map() function: A complex example
const employeesData = [
{
name: 'Saka manje',
address: '14, cassava-garri-ewa street',
attributes: {
height: '6ft',
hairColor: 'Brown',
eye: 'Black',
},
gender: 'Male',
},
{
name: 'Adrian Toromagbe',
address: '14, kogbagidi street',
attributes: {
height: '5ft',
hairColor: 'Black',
eye: 'Red',
}, //code_209_49-94
gender: 'Male',
},
CENG 475 ]