{"id":1208,"date":"2020-07-02T13:12:44","date_gmt":"2020-07-02T13:12:44","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=1208"},"modified":"2020-07-02T13:12:44","modified_gmt":"2020-07-02T13:12:44","slug":"javascript-concepts-to-master-before-learning-react","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=1208","title":{"rendered":"JavaScript concepts to master before learning React"},"content":{"rendered":"\n<p>As you likely already know, React is a library to create UI components that can be used as the basis of web and mobile applications. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>What distinguishes React from some of its competitors is that its code is written entirely with JavaScript. Even the HTML-like templates are written in JS using\u00a0<a href=\"https:\/\/facebook.github.io\/jsx\/\">JSX<\/a>, which is an extension of the JS language to structure UI components.<\/p>\n\n\n\n<p>The goal of this article is to help aspiring React developers get started by highlighting the JavaScript they ought to master before really diving into React. A complete introduction to JavaScript would go beyond the scope of this article, but React builds on the use of modern JavaScript features that were mainly introduced with&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/ECMAScript#6th_Edition_%E2%80%93_ECMAScript_2015\">ES2015<\/a>.<\/p>\n\n\n\n<p>Below, I give an overview of the common JavaScript patterns and language features that are heavily used in virtually every React application. For each concept, I provide external links. If you are interested, you can learn more about using it in React context.<\/p>\n\n\n\n<h2>Conditional logic with&nbsp;<code>if<\/code>&nbsp;statements, the ternary operator, and logical operators<\/h2>\n\n\n\n<p>These operators have been part of JavaScript for a very long time. In React, they are especially useful for conditional rendering of components.<\/p>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Conditional_Operator\">ternary operator<\/a>&nbsp;looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const buttonLabel = playback === \"stop\" ? \"play <img alt=\"\u25b6\ufe0f\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/25b6.svg\">\" : \"stop <img alt=\"\u23f9\ufe0f\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/23f9.svg\">\";<\/pre>\n\n\n\n<p>If the variable&nbsp;<code>playback<\/code>&nbsp;has the value&nbsp;<code>stop<\/code>, then the operator assigns to&nbsp;<code>buttonLabel<\/code>&nbsp;the string value&nbsp;<code>play&nbsp;<\/code>&nbsp;and, in all other cases, the string value&nbsp;<code>stop&nbsp;<\/code>. It is basically the same as the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">let buttonLabel;\nif (playback === \"stop\") {\n  buttonLabel = \"play <img alt=\"\u25b6\ufe0f\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/25b6.svg\">\";\n}\nelse {\n  buttonLabel = \"stop <img alt=\"\u23f9\ufe0f\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/23f9.svg\">\"\n}<\/pre>\n\n\n\n<p>Of course, you can use such an&nbsp;<code>if...else<\/code>&nbsp;statement, but the ternary operator is often the instrument of choice if you need to use a single line expression for&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/conditional-rendering.html#inline-if-else-with-conditional-operator\">conditionally rendering elements inline<\/a>.<\/p>\n\n\n\n<p>Otherwise, you have to call a&nbsp;<a href=\"https:\/\/codepen.io\/gaearon\/pen\/ZpVxNq?editors=0011\">function where you put your code for conditional rendering<\/a>. In React, you can also use more complex condition logic (e.g., an&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/if...else\"><code>if...else<\/code>&nbsp;cascade<\/a>) and store values in variables that can be used for&nbsp;<a href=\"https:\/\/codepen.io\/gaearon\/pen\/QKzAgB?editors=0010\">conditional rendering<\/a>&nbsp;in JSX code.<\/p>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Logical_Operators\">Logical operators<\/a>&nbsp;<code>&amp;&amp;<\/code>&nbsp;or&nbsp;<code>||<\/code>&nbsp;are very handy for building React components.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const isLoggedIn = true;\nconst userComponent = isLoggedIn &amp;&amp; getUserComponent();<\/pre>\n\n\n\n<p>In our example, the left operand (<code>isLoggedIn<\/code>) of the&nbsp;<code>&amp;&amp;<\/code>&nbsp;operator evaluates to&nbsp;<code>true<\/code>. Therefore, the result of the right operand (the function call&nbsp;<code>getUserComponent()<\/code>) gets assigned to the&nbsp;<code>userComponent<\/code>&nbsp;variable.<\/p>\n\n\n\n<p>This concept is also very useful for&nbsp;<a href=\"https:\/\/codepen.io\/gaearon\/pen\/ozJddz?editors=0010\">conditional rendering in React<\/a>&nbsp;because&nbsp;<code>true&nbsp;&amp;&amp;&nbsp;jsxComponent<\/code>&nbsp;returns&nbsp;<code>jsxComponent<\/code>, and&nbsp;<code>false&nbsp;&amp;&amp;&nbsp;jsxComponent<\/code>&nbsp;returns&nbsp;<code>false<\/code>. If you return&nbsp;<code>false<\/code>, React ignores it and simply renders nothing.<\/p>\n\n\n\n<p>It is also possible to combine multiple conditions. In the next example, the result of&nbsp;<code>getOtherUsersComponent()<\/code>&nbsp;is returned when both conditions are met.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const otherUsers = isLoggedIn &amp;&amp; users?.length &gt; 0 &amp;&amp; getOtherUsersComponent();<\/pre>\n\n\n\n<p>Notice the&nbsp;<code>?<\/code>&nbsp;in the second operand&nbsp;<code>users?.length&nbsp;&gt;&nbsp;0<\/code>. This is&nbsp;<a href=\"https:\/\/blog.logrocket.com\/optional-chaining-and-nullish-coalescing-in-javascript\/\">optional chaining<\/a>, which is not uncommon in React projects.<\/p>\n\n\n\n<p>If you return&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/null\">null<\/a><\/code>, React doesn\u2019t render anything. In contrast to&nbsp;<code>undefined<\/code>,&nbsp;<code>null<\/code>&nbsp;represents the intentional absence of any value.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">if (shouldRenderComponent()) {\n  return getComponent();\n}\nelse {\n  return null;\n}<\/pre>\n\n\n\n<p>This is useful to&nbsp;<a href=\"https:\/\/codepen.io\/gaearon\/pen\/Xjoqwm?editors=0010\">prevent components from rendering<\/a>.<\/p>\n\n\n\n<h2 id=\"objectliteralsandinlinefunctions\">Object literals and inline functions<\/h2>\n\n\n\n<p>There are&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Object_initializer\">multiple ways to create objects<\/a>. Initializing objects with the literal notation looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const foo = { bar: 3, hello: \"world\" };<\/pre>\n\n\n\n<p>This notation is frequently used in React projects to create objects inline without assigning them to a variable, e.g., for the initial state of&nbsp;<code>useReducer<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ 2nd function argument uses inline object literal\nfoo(\"bar\", { hello: \"world\" })<\/pre>\n\n\n\n<p>With ES2015 syntax, you can also use shorthand properties and method names.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ instead of \nfunction foo(id) {\n  return {\n    name: \"dummy\",\n    id: id,\n    bar: function() {\n      console.log(\"bar\");\n    }\n  }\n}\n\/\/ you can do\nfunction foo(id) {\n  return {\n    name: \"dummy\",\n    id, \/\/ shorthand property name\n    bar() { \/\/ shorthand method name\n      console.log(\"bar\");\n    }\n  }\n}<\/pre>\n\n\n\n<p>Shorthand properties especially are used all over the place in React development since they eliminate redundant code.<\/p>\n\n\n\n<p>That said, you have to be aware of the subtle difference between an inline object literal and a variable pointing to an object (created by an object literal). In some cases, for React performance optimization purposes, you should&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/react-keep-react-fast#avoid-object-literals\">avoid passing object literals<\/a>&nbsp;to React components because a new object is created every time, causing unnecessary re-renders.<\/p>\n\n\n\n<p>The same principle applies to anonymous functions (i.e., inline functions), which&nbsp;<a href=\"https:\/\/www.digitalocean.com\/community\/tutorials\/react-keep-react-fast#avoid-anonymous-functions\">should be avoided<\/a>&nbsp;in some React performance use cases.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ inline function\nfoo(() =&gt; {console.log(\"bar\")});\n\/\/ passing variable pointing to function\nconst barFunc = () =&gt; console.log(\"bar\");\nfoo(barFunc);<\/pre>\n\n\n\n<h2>Template literals<\/h2>\n\n\n\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Template_literals\">Template literals<\/a>, or template strings, were introduced with ES2015 and allow for creating strings with embedded JavaScript expressions. Within backticks, you can combine \u201chardcoded\u201d strings with JavaScript expressions inside of&nbsp;<code>${}<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const name = \"doppelmutzi\";\nconsole.log(`Hello, my name is ${name}`); \/\/ Hello, my name is doppelmutzi<\/pre>\n\n\n\n<p>Expressions can, of course, be more complex, like inline calculations or function calls.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const name = \"doppelmutzi\";\nconst getRandomIndex = max =&gt;  Math.floor(Math.random() * Math.floor(max))\nconst food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\nconst getFood = index =&gt; food[index]\nconsole.log(`Hello, my name is ${name} \nand I'm hungry for ${getFood(getRandomIndex(food.length))}`);<\/pre>\n\n\n\n<p>The latter example also uses the multiline feature so that the output has a line break after the expression interpolation (<code>${name}<\/code>).<\/p>\n\n\n\n<h2>Switch statement<\/h2>\n\n\n\n<p>In medium- and large-sized React applications, you\u2019ll most likely be confronted with the&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/switch\">switch<\/a><\/code>&nbsp;statement for managing state across components. Tools like the&nbsp;<code><a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usereducer\">useReducer<\/a><\/code>&nbsp;Hook or&nbsp;<a href=\"https:\/\/redux.js.org\/\">Redux<\/a>&nbsp;are often used for such tasks.<\/p>\n\n\n\n<p>The following example shows a&nbsp;<a href=\"https:\/\/medium.com\/async-la\/a-short-and-sour-guide-to-reducers-b5b54d3bb018\">so-called reducer function<\/a>&nbsp;using a&nbsp;<code>switch<\/code>&nbsp;statement for state management. You don\u2019t necessarily have to use&nbsp;<code>switch<\/code>&nbsp;statements with a reducer, but it is a well-established pattern.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">export default (state, action) =&gt; {\n  switch (action.type) {\n    case \"TOGGLE_DARK_MODE\":\n      return {\n        ...state,\n        darkMode: action.darkMode,\n      };\n     case \"UPDATE_PLAYBACK\": {\n      return {\n        ...state,\n        currentSound: action.currentSound,\n      };\n    }  \n    default:\n      return state;\n  }\n};<\/pre>\n\n\n\n<p>The example above checks the value of&nbsp;<code>action.type<\/code>&nbsp;and executes the code of a&nbsp;<code>case<\/code>&nbsp;statement. If it evaluates to a string,&nbsp;<code>TOGGLE_DARK_MODE<\/code>, then the code of the first case statement is executed.<\/p>\n\n\n\n<p>It is good practice to have an optional default clause. It gets executed if the&nbsp;<code>switch<\/code>&nbsp;expression matches none of the case clauses. Using the spread operator (e.g.,&nbsp;<code>...state<\/code>) is a common practice.<\/p>\n\n\n\n<p>In the above example, every case (and default) clause returns a new object, representing the new React state. This brings us to an important topic of React development.<\/p>\n\n\n\n<h2>Object destructuring<\/h2>\n\n\n\n<p>The principle of&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Destructuring_assignment\">object destructuring<\/a>&nbsp;is pretty simple. With the elegant syntax below, we can extract properties into variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const creatures = {\n  human: [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fe-200d-1f4bb.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bb.svg\">\"],\n  supernatural: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f916.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f479.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47a.svg\">\"]\n};\nconst { human, supernatural } = creatures;\nconsole.log(human); \/\/ [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fe-200d-1f4bb.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bb.svg\">\"]\nconsole.log(supernatural); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f916.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f479.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47a.svg\">\"]<\/pre>\n\n\n\n<p>If you use assignment without variable declaration, you need to use parentheses.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const creatures = {\n  human: [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fe-200d-1f4bb.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bb.svg\">\"],\n  supernatural: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f916.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f479.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47a.svg\">\"]\n};\nlet human, supernatural;\n({human, supernatural} = creatures);\nconsole.log(human); \/\/ [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fe-200d-1f4bb.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bb.svg\">\"]\nconsole.log(supernatural); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f916.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f479.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47a.svg\">\"]<\/pre>\n\n\n\n<p>Object destructuring offers you syntactical sugar to save extra lines of code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ you can do this\nconst { human, supernatural } = creatures;\n\/\/ instead of\nconst human = creatures.human;\nconst supernatural = creatures.supernatural;<\/pre>\n\n\n\n<p>In the context of React, object destructuring is frequently used with function parameters.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const infos = {name: \"doppelmutzi\", hobby: \"React\" };\nfunction printInfos({name, hobby}) {\n  console.log(name, hobby);\n}\nprintInfos(infos);\nconst printName = ({name}) =&gt; console.log(name);\nprintName(infos);<\/pre>\n\n\n\n<p>For cleaner code, React developers use this pattern with&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html\">props<\/a>, which are the input for React components.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function MyReactComponent({name, age}) {\n  \/\/ ...\n}<\/pre>\n\n\n\n<p>Assigning in combination with renaming variables might be useful to increase the readability of your code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const creatures = {\n  human: [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\"]\n};\nconst { human: people  } = creatures;\nconsole.log(people); \/\/ [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\"]<\/pre>\n\n\n\n<p>You can also define default values while unpacking fields from the assigned object. The following example combines multiple techniques.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const { human: people = [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\"], supernatural = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\"] } = {\n  human: [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\"]\n};\nconsole.log(people); \/\/ [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\"]\nconsole.log(supernatural); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f47d.svg\">\"]<\/pre>\n\n\n\n<p>Nesting is also possible, but I wouldn\u2019t recommend overdoing it; otherwise, understandability decreases.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const creatures = {\n  animals: {\n    wildlife: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f982.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f40d.svg\">\"],\n    pet: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f415.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f408.svg\">\"]\n  },\n  human: [\"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f468-1f3ff-200d-1f4bc.svg\">\", \"<img alt=\"??\u200d?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f469-1f3fc-200d-1f4bc.svg\">\", \"<img alt=\"??\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9d1-1f3fb.svg\">\u200d<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f4bc.svg\">\"]\n};\nconst { animals: { pet }} = creatures;\nconsole.log(pet); \/\/  [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f415.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f408.svg\">\"]<\/pre>\n\n\n\n<p>Ronald Chen provides some&nbsp;<a href=\"https:\/\/medium.com\/@pyrolistical\/destructuring-nested-objects-9dabdd01a3b8\">more insights<\/a>&nbsp;on destructuring nested objects.<\/p>\n\n\n\n<h2>Array destructuring<\/h2>\n\n\n\n<p>With the help of the&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Destructuring_assignment\">destructuring assignment<\/a>, an array can be unpacked in such a way that its values are extracted into distinct variables, like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const array = [1, 2];\nconst [varForVal1, varForVal2] = array;\nconsole.log(varForVal1); \/\/ 1\nconsole.log(varForVal2); \/\/ 2<\/pre>\n\n\n\n<p>As you can see, variables are assigned from the left to the right of the array, so order is maintained.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [fruit, veggie] = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\"];\nconsole.log(fruit); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\nconsole.log(veggie); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\"><\/pre>\n\n\n\n<p>You can also skip values under consideration of the order.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [fruit,,pizza,,,burger] = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\"];\nconsole.log(fruit); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\nconsole.log(pizza); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\nconsole.log(burger); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\"><\/pre>\n\n\n\n<p>The following \u201cchatty code\u201d might help to understand what exactly happened.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [\n  fruit,\n  \/* skip entry 2 (<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">) *\/,\n  pizza,\n  \/* skip entry 4 (<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">) *\/,\n  \/* skip entry 5 (<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">) *\/,\n  burger] = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\"];<\/pre>\n\n\n\n<p>You can also assign multiple values at once with the rest pattern.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const [fruit, veggie, ...junkfood] = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\"];\nconsole.log(fruit); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\nconsole.log(veggie); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\nconsole.log(junkfood); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\"]<\/pre>\n\n\n\n<p>Array destructuring allows for default values. Of course, you can combine this pattern with function calls, too.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = () =&gt; [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\"];\nconst [fruit, veggie, junkfood = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"] = getFood();\nconsole.log(fruit); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\nconsole.log(veggie); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\nconsole.log(junkfood); \/\/ <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\"><\/pre>\n\n\n\n<p>The elegant concept of array destructuring is used frequently with&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/hooks-overview.html\">React Hooks<\/a>&nbsp;because you can come up with a few lines of semantic code. To create a state variable along with an updater function for a React component, you can use React\u2019s&nbsp;<code><a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usestate\">useState<\/a><\/code>&nbsp;Hook.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const initialValue = false;\n\/\/ instead of \nconst stateWithUpdater = useState(initialValue);\nconst darkMode = stateWithUpdater[0];\nconst darkModeUpdater = stateWithUpdater[1];\n\/\/ you can do\nconst [darkMode, setDarkMode] = useState(initialValue);<\/pre>\n\n\n\n<p>The following example demonstrates that you can implement generic functions for concrete use cases. Array destructuring allows the function caller to use semantic variable names.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = type =&gt; {\n  let food = [];\n  let error = false;\n  if (type === \"fruits\") {\n    food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\"];\n  }\n  else if (type === \"junk\") {\n    food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\"];\n  }\n  else {\n    error = true;\n  }\n  const addFood = newFood =&gt; food.push(newFood);\n  return [food, error, addFood];\n};\nconst [healthyFood, noFruitsAvailable, addFruitFunc] = getFood(\"fruits\");\nconsole.log(healthyFood); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\"]\nconsole.log(noFruitsAvailable); \/\/ false\nconsole.log(addFruitFunc(\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\")); \nconsole.log(healthyFood); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\"]<\/pre>\n\n\n\n<p>For this use case, in my opinion, returning an array with the&nbsp;<code>getFood<\/code>&nbsp;function leads to more concise code than with object destructuring. Array destructuring allows for custom variable names. In contrast, with object destructuring, you need to rename the variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = type =&gt; {\n  \/\/ same function body as above, only different return statement\n  return {food, error, addFood};\n};\nconst {food: healthyFood, error: noFruitsAvailable, addFood: addFruitFunc} = getFood(\"fruits\");\nconsole.log(noFruitsAvailable); \/\/ false\nconsole.log(addFruitFunc(\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\")); \nconsole.log(healthyFood); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\"]<\/pre>\n\n\n\n<p>That\u2019s probably why&nbsp;<code>useState<\/code>&nbsp;returns an array and not an object \u2014 to be more generic.<\/p>\n\n\n\n<h2>Spread operator<\/h2>\n\n\n\n<p>The&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/Spread_syntax\">spread operator<\/a>&nbsp;(<code>...<\/code>) allows an iterable item (e.g., an array) to be extracted into its parts and plugged into places that expect individual elements. With this syntax, you can split up object properties or array elements.<\/p>\n\n\n\n<p>In the next use case below, we pull out elements of an array and pass each element as individual function argument.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const numbers = [11, 5, 3, 1, 26];\n\/\/ Math.max expects to be called like Math.max(11,5,3,1,26)\nconsole.log(Math.max(...numbers)); \/\/ 26<\/pre>\n\n\n\n<p>Another use case is to copy object properties and, thus, create a new object.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const food = {\n  breakfast: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\"],\n  lunch: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"]\n};\nconst foodAndDrinks = {\n  ...food,\n  drinks: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f377.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f379.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f37a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f943.svg\">\"],\n};\nconsole.log(foodAndDrinks); \n\/* \n{\n  breakfast: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\"],\n  lunch: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"],\n  drinks: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f377.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f379.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f37a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f943.svg\">\"],\n} *\/<\/pre>\n\n\n\n<p>With this succinct syntax, you can conveniently create a copy of an array.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">  const food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n  const copy = [...food];\n  console.log(copy); \/\/ [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"]\n  console.log(food === copy); \/\/ false<\/pre>\n\n\n\n<p>Recalling the last paragraph of our subsection on the switch statement, the spread syntax is frequently used in the context of React state. With React, you should not manipulate state objects directly. Instead, you need to create a brand-new state object whenever you want to update the state. The following concept is considered good practice.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const restaurantState = {\n  drinks: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f377.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f379.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f37a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f943.svg\">\"],\n  food: [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"],\n  lastOrder: null\n}\n\/\/ the customer ordered a <img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\nconst stateAfterOrder = {\n  drinks: [...restaurantState.drinks], \/\/ copy drinks\n  food: [...restaurantState.food], \/\/ copy food\n  lastOrder:  \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\" \/\/ override lastOrder\n}<\/pre>\n\n\n\n<h2>Rest operator<\/h2>\n\n\n\n<p>With the help of the&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/rest_parameters\">rest operator<\/a>&nbsp;(<code>...<\/code>), you can merge a list of function arguments into an array. While the syntax of the rest operator is the same as the spread operator, their place of use makes all the difference.<\/p>\n\n\n\n<p>If you use the operator inside of&nbsp;<code>{}<\/code>&nbsp;or&nbsp;<code>[]<\/code>, you use object or array spreading, respectively. On the other hand, if you use the operator with the last argument in a function signature, that is the rest parameter.<\/p>\n\n\n\n<p>Its purpose is to merge a list of values into an array. In the next example, various food is provided as function arguments. With the rest operators, they are put into an array called&nbsp;<code>food<\/code>. The&nbsp;<code>findIndex<\/code>&nbsp;method is operating on an array and tests whether broccoli is included.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const cleanFoodIncluded = (...food) =&gt; food.findIndex(f =&gt; f === \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\") !== -1;\nconsole.log(cleanFoodIncluded(\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f32e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f96a.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f966.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\")); \/\/ true<\/pre>\n\n\n\n<p>It is a common practice to use the rest operator in combination with the spread operator. This combines multiple arguments into an array to distribute the entries again in another place inside of a React component.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function renderProduct(\n  \/\/ these are considered as component-specific\n  {id, name}, \n\/* all other arguments are relevant for container component. Therefore, consolidate them into an array with rest operator *\/\n...containerProps) { \n  \/\/ output 0815, choco, [{margin: \"10px\"}, {padding: \"5px\"}]\n    console.log(id, name, containerProps); \n  \/* unpack array again with spread operator to provide them as individual args *\/\n  renderContainer(...containerProps); \n\n  }\nfunction renderContainer(margin, padding) {\n  \/\/ output {margin: \"10px\"}, {padding: \"5px\"}\n  console.log(margin, padding); \n}\n  const product = {\n    id: \"0815\", name: \"choco\"\n  }\n  renderProduct(product, {margin: \"10px\"}, {padding: \"5px\"} );<\/pre>\n\n\n\n<h2>Function declarations, function expressions, and arrow functions<\/h2>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/function\">function declaration<\/a>&nbsp;looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function getFood(index) {\n  const food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n  return food[index];\n}<\/pre>\n\n\n\n<p>In contrast, this is a&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/function\">function expression<\/a>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = function(index) {\n  const food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n  return food[index];\n}<\/pre>\n\n\n\n<p>An&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Functions\/Arrow_functions\">arrow function expression<\/a>&nbsp;constitutes an alternative to the former two. Its advantage is in its syntactical sugar, which allows you to write functions in a more concise manner.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = (index) =&gt; {\n  const food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n  return food[index];\n}<\/pre>\n\n\n\n<p>If you have only one parameter, you can skip the parentheses.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = index =&gt;  {\n  const food = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n  return food[index];\n}<\/pre>\n\n\n\n<p>If your function does not accept any parameter, you need to use parentheses.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = () =&gt;  {\n  return [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"];\n}<\/pre>\n\n\n\n<p>If the function body consists of a single return statement, you can replace this explicit return with an implicit return, like so:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const getFood = index =&gt;  \\[\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95e.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f9c7.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"\\][index];<\/pre>\n\n\n\n<p>Only with function declarations can you invoke functions before they have even been defined. This is because function declarations are hoisted, i.e., they are moved to the top of their scope before execution.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">console.log(getFoodDeclaration()); \/\/ \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\"\nconsole.log(getFoodExp()); \/\/ ReferenceError: Cannot access 'getFoodExp' before initialization\nconsole.log(getFoodArrow()); \/\/ ReferenceError: Cannot access 'getFoodArrow' before initialization\nfunction getFoodDeclaration() {\n  return \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f35f.svg\">\";\n}\nconst getFoodExp = () =&gt;  {\n  return \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f354.svg\">\";\n}\nconst getFoodArrow = () =&gt;  \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\";<\/pre>\n\n\n\n<p>Another difference between function declarations\/expressions and arrow function expressions is the&nbsp;<code>this<\/code>&nbsp;keyword, which I\u2019ll discuss in the context of classes.<\/p>\n\n\n\n<h2>Classes<\/h2>\n\n\n\n<p>A&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Statements\/class\">class<\/a>&nbsp;represents a blueprint for new objects. Variables and functions can be attached to a class and are called properties and methods, respectively. In the context of a class, the&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/this\">this<\/a><\/code>&nbsp;keyword refers to the current instance. If you are coming from an object-oriented background, you most likely have some incorrect assumptions about it;&nbsp;<a href=\"https:\/\/www.vojtechruzicka.com\/javascript-this-keyword\/\">this article<\/a>&nbsp;helps to demystify the&nbsp;<code>this<\/code>&nbsp;keyword.<\/p>\n\n\n\n<p>A class can have a&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Classes\/constructor\">constructor<\/a>&nbsp;method, which represents a special kind of function to initialize new objects of the blueprint. You can instantiate the class with the&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/new\">new<\/a><\/code>&nbsp;keyword. With this, the constructor is invoked (or the default constructor, if you do not provide any), and a new object is created.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Fruit {\n  \/\/ class body\n  constructor() {\n    \/\/ property\n    this.popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\"\n  }\n  whatsPopular() {\n    \/\/ method body\n    console.log(this.popular) \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\"\n  }\n}\n\/\/ instantiate an object from the class\nconst fruit = new Fruit();\n\/\/ call the method on the instance\nfruit.whatsPopular();<\/pre>\n\n\n\n<p>Another crucial concept is&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/JavaScript\/Objects\/Inheritance\">inheritance with the class syntax<\/a>. With the&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Operators\/super\">super<\/a><\/code>&nbsp;keyword, you can access the parent.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Food {\n  constructor() {\n    this.popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"\n  }\n}\nclass Fruit extends Food {\n  constructor() {\n    \/\/ required to call constructor of parent class\n    \/\/ needs to be first statement in child constructor\n    super();\n    \/\/ override\n    this.popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\"\n  }\n  whatsPopular() {\n    console.log(this.popular) \/\/ \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\"\n  }\n}\nconst fruit = new Fruit();\nfruit.whatsPopular();<\/pre>\n\n\n\n<p>With&nbsp;<a href=\"https:\/\/www.dotnetcurry.com\/javascript\/1405\/es8-es2017-javascript-new-features\">ES2017<\/a>, the syntax for using class properties and methods is a&nbsp;<a href=\"https:\/\/medium.com\/@charpeni\/arrow-functions-in-class-properties-might-not-be-as-great-as-we-think-3b3551c440b1\">little bit more concise<\/a>. You can use arrow functions as methods, too.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Food {\n  popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\";\n}\nclass Fruit extends Food {\n  popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\";\n  \/\/ method by arrow function expression\n  whatsPopular = () =&gt; {\n    console.log(this.popular)\n  };\n}\nconst fruit = new Fruit();\nfruit.whatsPopular();<\/pre>\n\n\n\n<p>Often, when you come across&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html#function-and-class-components\">classes in React development<\/a>, you will find the ES2017 syntax.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ class-based React components must extend React.Component (or a subclass like React.PureComponent)\nclass Food extends React.Component {\n  \/\/ define default React state with ES2017 property syntax\n  state = {\n    popular = \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\"\n  }\n  render() {\n    \/\/ required method by every class-based React component\n  }\n}<\/pre>\n\n\n\n<p>Note that this section is by no means a complete explanation of JavaScript classes. In my opinion, you do not need to devote too much time to learning classes if your plan is to learn React. My recommended learning path for React beginners is to understand the basics of classes as presented here to be able to read and understand class-based React components.<\/p>\n\n\n\n<p>I think understanding classes extensively for new React developers is not necessary because the importance of classes has decreased drastically since last year. This is related to the introduction of React Hooks.<\/p>\n\n\n\n<p>Before then, it was only possible to have sophisticated React components with the class-based approach. Only with classes was it possible to define component state and use lifecycle methods. Hooks allow similar things with functional components, too. The whole React community is strongly pushing towards only using function components.<\/p>\n\n\n\n<p>However, if you work on legacy projects with existing class-based components or if you come across some of the&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/hooks-faq.html#do-hooks-cover-all-use-cases-for-classes\">few use cases<\/a>&nbsp;that require you to use classes, or even if you just like to use classes (e.g., use a specific lifecycle method), then you need to understand the React-related class foundations.<\/p>\n\n\n\n<p>Luckily, for React development, the relevant aspects of JavaScript classes are not very complicated. I like to use the ES2017 syntax for class components and arrow functions for methods because they&nbsp;<a href=\"https:\/\/medium.com\/@joespinelli_6190\/using-arrow-functions-to-avoid-binding-this-in-react-5d7402eec64\">do not require<\/a>&nbsp;use of the&nbsp;<code>bind()<\/code>&nbsp;method. The code gets more understandable.<\/p>\n\n\n\n<p>Consider the first example, which requires a&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Function\/bind\">bind<\/a><\/code>; otherwise, the invocation of&nbsp;<code>this.setState<\/code>&nbsp;causes an error.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Button extends React.Component {\n  constructor(props) {\n    super(props);\n    this.state = { clicked: false };\n    \/\/ this.onClick = this.onClick.bind(this);\n  }\n  onClick() {\n    this.setState({ clicked: true }); \/\/ ERROR this.setState is not a function\n  }\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;p&gt;{this.state.clicked &amp;&amp; \"clicked\"}&lt;\/p&gt;\n        &lt;button onClick={this.onClick}&gt;click&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/pre>\n\n\n\n<p>With the ES2017 syntax, you can write more understandable class-based components. The reason is because arrow functions have a&nbsp;<a href=\"https:\/\/hackernoon.com\/javascript-es6-arrow-functions-and-lexical-this-f2a3e2a5e8c4\">lexical&nbsp;<code>this<\/code><\/a>, and its value within the arrow function is determined by the surrounding scope (in our case, the&nbsp;<code>render<\/code>&nbsp;method that has access to state with&nbsp;<code>this<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Button extends React.Component {\n  state = {\n    clicked: false\n  }  \n  onClick = () =&gt; {\n    this.setState({ clicked: true });\n  }\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;p&gt;{this.state.clicked &amp;&amp; \"clicked\"}&lt;\/p&gt;\n        &lt;button onClick={this.onClick}&gt;click&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/pre>\n\n\n\n<h2>Array functions<\/h2>\n\n\n\n<p>Mastering&nbsp;<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\">array functions<\/a>&nbsp;is an important skill for React developers.&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/map\">map()<\/a><\/code>&nbsp;is used in basically every React application, e.g., to loop over a list of food objects and render every single entry within an&nbsp;<code>li<\/code>&nbsp;tag.<\/p>\n\n\n\n<p>The&nbsp;<code>map()<\/code>&nbsp;function produces a new array with the same number of elements. However, for every entry of the original entry, an operation was applied to produce new entries. The following example creates a new array with duplicated fruits for every entry.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const fruits = &#91;\"\", \"\", \"\", \"\"];\nconst moreFruits = fruits.map(f => `${f}${f}`);\nconsole.log(moreFruits); \/\/ &#91;\"\", \"\", \"\", \"\"]\n<\/code><\/pre>\n\n\n\n<p><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/filter\">filter()<\/a><\/code>&nbsp;is often used with state management because it returns a brand-new array containing only those elements of the original array that pass a provided condition.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const people = [\n  { name: \"Max\", sex: \"male\" }, \n  { name: \"Jacky\", sex: \"female\" },\n  { name: \"Stephanie\", sex: \"female\" }\n];\nconst women = people.filter(person =&gt; person.sex === \"female\");\nconsole.log(women); \/*  [{ name: \"Jacky\", sex: \"female\" }, { name: \"Stephanie\", sex: \"female\"}] *\/..<\/pre>\n\n\n\n<p><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/findIndex\">findIndex()<\/a><\/code>&nbsp;returns the index of the first element that passes the test; otherwise, it returns&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const fruits = [\"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f353.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\", \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\"];  \nconsole.log(fruits.findIndex(fruit =&gt; fruit === \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f95d.svg\">\")); \/\/ 1\nconsole.log(fruits.findIndex(fruit =&gt; fruit === \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\"><img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f34c.svg\">\")); \/\/ -1\nconsole.log(fruits.findIndex(fruit =&gt; fruit === \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f352.svg\">\")); \/\/ 2 (first match)<\/pre>\n\n\n\n<p><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/TypedArray\/find\">find()<\/a><\/code>&nbsp;returns the first entry that passes the test. This is useful in the context of React state management. As an example, we have a list of users. We clicked on a particular user in a list and want to display a modal dialog showing this user\u2019s information.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const users = [\n  { id: 1, name: \"Max\", sex: \"male\" },\n  { id: 2, name: \"Jacky\", sex: \"female\" },\n  { id: 3, name: \"Stephanie\", sex: \"female\" }\n];\nfunction logUserInfo(id) {\n  console.log(users.filter(user =&gt; user.id === id));\n}\nlogUserInfo(2); \/\/ { id: 2, name: \"Jacky\", sex: \"female\" }<\/pre>\n\n\n\n<h2>Immutable vs. mutable values<\/h2>\n\n\n\n<p>This concept is important to understand. Immutable values and objects cannot be changed afterwards, so the original remains untouched. Primitive values like strings or numbers are immutable by nature. On the other hand, objects are mutable by default. Let\u2019s take a look what this means.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ immutable use case\n\/\/ Change strings won't work. Throws error in strict mode\n\"use strict\";\nconst hello = \"world\";\nhello[0] = \"W\"; \/\/ try to upper case the first char\nconsole.log(hello); \/\/ world (in none-strict mode)<\/pre>\n\n\n\n<p>A misguided mindset about mutability can lead to bugs.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ mutability use case\nconst meal = {\n  kind: \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\">\",\n  origin: {\n    country: \"Italy\"\n  }\n}\nconst fruit = {\n  kind: \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f347.svg\">\",\n  origin: meal.origin\n};\nconsole.log(`${fruit.kind} from ${fruit.origin.country}`); \/\/ <img alt=\"\u2705\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/2705.svg\"> \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f347.svg\"> from Italy\"\nconsole.log(`${meal.kind} from ${meal.origin.country}`); \/\/ <img alt=\"\u2705\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/2705.svg\">  \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\"> from Italy\"\n\/\/ we bought new grapes from Germany\nfruit.origin.country = \"Germany\";\nconsole.log(`${fruit.kind} from ${fruit.origin.country}`); \/\/ <img alt=\"\u2705\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/2705.svg\">  \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f347.svg\"> from Germany\"\n\/\/ we have caused an unwanted side effect\nconsole.log(`${meal.kind} from ${meal.origin.country}`); \/\/ <img alt=\"\u274c\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/274c.svg\"> \"<img alt=\"?\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/1f355.svg\"> from Germany\"<\/pre>\n\n\n\n<p>Objects are mutable, but you can use&nbsp;<code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Object\/freeze\">Object.freeze()<\/a><\/code>&nbsp;or third-party libraries like&nbsp;<a href=\"https:\/\/immutable-js.github.io\/immutable-js\/\">Immutable.js<\/a>&nbsp;to make them immutable.<\/p>\n\n\n\n<p>The React team recommends you use immutable objects in multiple areas of your application design, e.g., with&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/state-and-lifecycle.html#using-state-correctly\">component-based<\/a>&nbsp;and&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/hooks-reference.html#usereducer\">global state<\/a>. This is because immutability typically leads to&nbsp;<a href=\"https:\/\/stackoverflow.com\/a\/34385684\">multiple architectural benefits<\/a>. And that\u2019s why most of the developer community suggests pursuing a coding mindset of immutablity.<\/p>\n\n\n\n<p>I need to emphasize that mutation is not bad in itself. One problem with undisciplined mutation, however, is that it can lead to unexpected side effects, which are often the cause of unexpected bugs.<\/p>\n\n\n\n<p>In React development, do not try to mutate state variables directly; rather, use the preferred method of the state management library. As an example, the following code snippet shows how you should and how you should not update the local state of a class-based React component.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">class Button extends React.Component {\n  state = {\n    clicked: false\n  }  \n  onClick = () =&gt; {\n    \/\/ <img alt=\"\u274c\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/274c.svg\"> don't do this\n    this.state.clicked = true;\n    \/\/ <img alt=\"\u2705\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/2705.svg\"> instead do this: pass a new object to setState\n    this.setState({ clicked: true });\n  }\n  render() {\n    return (\n      &lt;div&gt;\n        &lt;p&gt;{this.state.clicked &amp;&amp; \"clicked\"}&lt;\/p&gt;\n        &lt;button onClick={this.onClick}&gt;click&lt;\/button&gt;\n      &lt;\/div&gt;\n    );\n  }\n}<\/pre>\n\n\n\n<p>If you use global state management tools like&nbsp;<code>useReducer<\/code>&nbsp;or&nbsp;<a href=\"https:\/\/redux.js.org\/\">Redux<\/a>, you should update state like this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">const newState = {\n  ...state, \/\/ creates a copy of the current state\n  darkMode: action.darkMode, \/\/ just override to reflect the changes\n};<\/pre>\n\n\n\n<h2>Callback functions<\/h2>\n\n\n\n<p>A function passed as an argument to another function is called a&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Callback_(computer_programming\">callback<\/a>&nbsp;if the function invokes the argument at a later time.<\/p>\n\n\n\n<p><code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/WindowOrWorkerGlobalScope\/setTimeout\">setTimeout<\/a><\/code>&nbsp;gets a callback function as the first argument that is called after the timer has expired (second argument).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">window.setTimeout(() =&gt; console.log(\"I'm a callback function\"), 1000); \/\/ after 1s: \"I'm a callback function\"<\/pre>\n\n\n\n<p>In the context of React,&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/faq-functions.html\">callback functions are often passed as props<\/a>&nbsp;to React components. Thereby, child components can execute passed callbacks at a later time in a way that parent components can react to it (e.g., update state and, thus, update the UI).<\/p>\n\n\n\n<p>Callback functions are also important in the context of&nbsp;<a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\">React Hooks<\/a>, e.g., to trigger side effects with&nbsp;<code><a href=\"https:\/\/reactjs.org\/docs\/hooks-effect.html\">useEffect<\/a><\/code>.<\/p>\n\n\n\n<h2>Conclusion<\/h2>\n\n\n\n<p>Since React development consists mainly of writing vanilla JavaScript code, I recommend acquiring a good understanding of JavaScript fundamentals before learning React. With this learning path, you will have a much smoother start \u2014 I promise you.<\/p>\n\n\n\n<h2>Full visibility into production React apps<\/h2>\n\n\n\n<p>Debugging React applications can be difficult, especially when users experience issues that are difficult to reproduce. If you\u2019re interested in monitoring and tracking Redux state, automatically surfacing JavaScript errors, and tracking slow network requests and component load time,&nbsp;<a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\">try LogRocket<\/a>.&nbsp;<a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\" target=\"_blank\" rel=\"noreferrer noopener\">LogRocket<\/a>&nbsp;is like a DVR for web apps, recording literally everything that happens on your React app. Instead of guessing why problems happen, you can aggregate and report on what state your application was in when an issue occurred. LogRocket also monitors your app&#8217;s performance, reporting with metrics like client CPU load, client memory usage, and more.<\/p>\n\n\n\n<p>The LogRocket Redux middleware package adds an extra layer of visibility into your user sessions. LogRocket logs all actions and state from your Redux stores.<\/p>\n\n\n\n<p>Modernize how you debug your React apps \u2014&nbsp;<a href=\"https:\/\/www2.logrocket.com\/react-performance-monitoring\" target=\"_blank\" rel=\"noreferrer noopener\">start monitoring for free<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As you likely already know, React is a library to create UI components that can be used as the basis of web and mobile applications.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[95],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1208"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1208"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}