Example:
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map((number) => number * 2);
console.log(doubled);
In the example above we use the
map()function to take an array of numbers and double their values. We assign the new array returned bymap()to the variable doubled and log it.
JSX using curly braces {}.And we loop through them by using the JavaScript map() function.Which will return an html element for each time, and we assign the resulting array of elements to the variable we have declare it.Example:
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li>{number}</li>
);
Below, we loop through the numbers array using the
map() function. We return a<li>element for each item. Finally, we assign the resulting array of elements tolistItems.
key wich is a special string attribute you need to include when creating lists of elements.Example:
const numbers = [1, 2, 3, 4, 5];
const listItems = numbers.map((number) =>
<li key={number.toString()}>
{number}
</li>
);
Keys help React identify which items have changed, are added, or are removed, and it should be given to the elements inside the array to give the elements a stable identity.===================================================
spread syntax refers to the use of an ellipsis of three dots (…) to expand an iterable object into the list of arguments.When …arr is used in the function call, it
expandsan iterable object arr into the list of arguments.
[...["1","2","3"]] //Array1[..."hi","hello","haha"] //Array2const hello = {hello: "1","2","3"}const world = {world: "hi","hello","haha"}const helloWorld = {...hello,...world}console.log(helloWorld) Object { hello: "1","2","3", world: "hi","hello","haha"}const fruits = ['apple','orange','banana','watermelon']const moreFruits = [...fruits];console.log(moreFruits)Array(5) ['apple','orange','banana','watermelon']fruits[0] = 'peach'console.log(...[...fruits,'...',...moreFruits])
//Result
['peach','apple','orange','banana','watermelon'] ... ['apple','orange','banana','watermelon']const objectOne = {hello: "hello"}const objectTwo = {world: "world"}const objectThree = {...objectOne, ...objectTwo, laugh: "Hahaha"}console.log(objectThree)Object { hello: "hello", world: "world", laugh: "Hahaha" }const objectFour = {...objectOne, ...objectTwo, laugh: () =>{console.log("Hahaha".repeat(5))}}objectFour.laugh() HahahaHahahaHahahaHahahaHahaha=================================================
## How to Pass Functions Between Components
### In the video, what is the first step that the developer does to pass functions between components?
(the name of the array),Then he return the created object that will access to each object inside the array so it will be modified.increment function do?.As in the video
this.props.increament(this.props.name);
and in the parent component we pass it as any other props by name and the value that refrence to the funtion we created to change the state of the objects.
As in the video
increament={this.increament};
References:
@React/React Docs - lists and keys
@Dr. Derek Austin/How to Use the Spread Operator (…) in JavaScript
@Steve Griffith - Prof3ssorSt3v3/How to Pass Functions Between Components