CS220 W5 Spring24
CS220 W5 Spring24
1
Arrow function
2
JS Function
JavaScript has
multiple ways to
declare functions.
•Function statement
•Arrow function
3
Arrow Function
A new way to write a function
A feature of ES6.
With arrow functions, you can create functions without using the
function keyword.
You also sometimes do not have to use the return keyword.
4
Arrow function Arguments
5
Refactoring function declarations
const App = () => {
return ( ... );
};
const Search = () => {
return ( ... );
};
const List = () => {
return ( ... );
};
6
Refactoring JS callback function
7
Block body VS Concise body
If an arrow function’s only purpose is
to return a value and it doesn’t have
any business logic in between,
you can remove the block body
(curly braces) of the function.
In a concise body, an implicit return
statement is attached, so you can
remove the return statement
8
Refactoring to concise body
const App = () => (
<div>
...
</div>
);
const Search = () => (
<div>
...
</div>
);
const List = () => (
<ul>
{list.map((item) => (
<li key={item.objectID}>...</li>
))}
</ul>
);
9
Arrow functions
11
Refactoring concept
13
Wrap-up
14
Wrap-up
17
Implementing User Interactions
20
Refactor Search Component to Block Body
const Search = () => {
// perform a task in between
return (
<div>
<label htmlFor="search">Search: </label>
<input id="search" type="text" />
</div>
);
};
21
Handler Function in JSX
23
Change Event
27
Handling User Interactions