<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by saidshah Ahmadi on Medium]]></title>
        <description><![CDATA[Stories by saidshah Ahmadi on Medium]]></description>
        <link>https://medium.com/@saidshahahmadi?source=rss-ab713604c088------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fyNtpglzymqIBr4vbXIttA.jpeg</url>
            <title>Stories by saidshah Ahmadi on Medium</title>
            <link>https://medium.com/@saidshahahmadi?source=rss-ab713604c088------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 23:04:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@saidshahahmadi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React for Beginners]]></title>
            <link>https://saidshahahmadi.medium.com/react-for-beginners-18ad91cbd761?source=rss-ab713604c088------2</link>
            <guid isPermaLink="false">https://medium.com/p/18ad91cbd761</guid>
            <dc:creator><![CDATA[saidshah Ahmadi]]></dc:creator>
            <pubDate>Mon, 05 Jul 2021 19:22:21 GMT</pubDate>
            <atom:updated>2021-07-05T19:32:11.651Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*6J3lz3Ghxw_BSdzwP853Xg.png" /></figure><p>In this blog post, I will explain basic concepts of React.js, and I will also clarify some of the most asked questions about them:</p><p>- <strong>What is React?</strong></p><p>- <strong>Why we use react?</strong></p><p>- <strong>Who uses React?</strong></p><p>- <strong>React Vs Angular Vs Vue</strong></p><p>- <strong>What is a state?</strong></p><p>- <strong>What is props?</strong></p><p>- <strong>How to handle HTML?</strong></p><p><strong>More ….</strong></p><blockquote><em>We will also learn about these topics</em></blockquote><p>· <strong>Create a New React App</strong></p><p>· <strong>Extension for making our code more easer to build React App</strong></p><p>· <strong>Application structure</strong></p><p>· <strong>Components</strong></p><p>· <strong>Virtual DOM</strong></p><p>· <strong>What is JSX?</strong></p><p>· <strong>Exploring our first React component — &lt;App/&gt;</strong></p><p>· <strong>Export statements</strong></p><p>· <strong>React State</strong></p><p>· <strong>How to install bootstrap</strong></p><p>· <strong>Styling</strong></p><p>· <strong>Rendering Classes Dynamically</strong></p><p>· <strong>Reading List</strong></p><p>· <strong>Conditional Rendering</strong></p><p>· <strong>Handling Events In React</strong></p><p>· <strong>Updating the state</strong></p><p>· <strong>what happens when the state changes.</strong></p><p>· <strong>React Hooks</strong></p><p>· <strong>Props</strong></p><p>· <strong>What are props in React?</strong></p><p>· <strong>Props vs state<br>more …</strong></p><h3>What is React?</h3><blockquote>React is a <strong>JavaScript library</strong> for building fast and interactive use interfaces.</blockquote><blockquote>It was developed at <strong>Facebook</strong> in 2011 and currently, it’s the most popular JS library for building user interfaces.</blockquote><h3>Why we use React?</h3><p>Here is a comparison of the <strong>top 3 libraries/frameworks</strong> for building user interfaces: <strong>React, Angular and Vue.</strong></p><h3>React Vs Angular Vs Vue</h3><p>As you can see, it is dominating the space, so if you want to expand your job opportunities as a front-end developer, you should have it on your resume. All of them are used for <strong>User-Interface</strong></p><p>Being a <strong>front-end developer</strong> is incredible right now. We can pick between lots of awesome JS frameworks.</p><p>I like to use React because Facebook’s React has changed the way we think about web applications and user interface development.</p><h3>Why we use react? therefor</h3><p>There are many big reasons why React has become so popular. But the main ones are:</p><p>- <strong>Clean programming</strong> — by creating your views with React, you get <strong>easy-to-read</strong> and <strong>easy-to-reuse</strong> code.</p><p>- <strong>Fast performance</strong> — React for small and large projects work very quickly thanks to the <strong>Virtual DOM</strong> it uses.</p><p>- <strong>Still using JavaScript — </strong>we still using JavaScript, so it’s not like learning something that can be applied to another framework in the feature.</p><p><strong>Who uses React?</strong></p><p>Many large-scale companies are using React in production. This is very encouraging when deciding to use React for your own projects, big and small,</p><p><strong>Here are some of the biggest sites built with React.</strong></p><p>- Netflix</p><p>- Facebook</p><p>- Instagram</p><p>- Airbnb</p><p>- Cloudflare</p><p>- Dropbox</p><p>- BBC</p><p>- Postmates</p><p>- Reddit</p><p><strong>And the list goes on !!!.</strong></p><p>Another cool thing about learning React is that we can use that knowledge to build <strong>Mobile Apps with React Native.</strong></p><p>- Facebook and Instagram</p><p>- Walmart</p><p>- Bloomberg</p><p>- Tasla</p><p>- Wix</p><p><strong>In 2019, React is one of the popular JavaScript frontend frameworks.</strong></p><p><strong>According to stack Overflow’s, the developers that loved it. Also, on GitHub stars.</strong></p><h3>Create a New React App</h3><blockquote>To install react we will need to make sure that our development environment is ready for us to start using Create React App.</blockquote><p>1- Install <strong>Node.js</strong> and <strong>NPM</strong></p><p>2- Code editor</p><p>Both are used to manage libraries (<strong>node packages</strong>) you will need along the way. These node packages can be libraries or whole frameworks. We’ll install external <strong>node packages</strong> via <strong>npm</strong> (<strong>node package manager</strong>).</p><p><em>You can verify node and npm versions in the command line using the </em><strong>node — version</strong><em> command.</em></p><p><strong>Command-line</strong></p><p><strong>npm –v</strong> <br> 6.14.8<br> <strong>node –v</strong></p><p>v14.15.0</p><p><strong>Now to install react on your machine see documentation of React.js </strong><a href="https://reactjs.org/docs/create-a-new-react-app.html"><strong>https://reactjs.org/docs/create-a-new-react-app.html</strong></a></p><p><strong>Extension for making our code more easer to build React App.</strong></p><p><strong>1.</strong> <strong>Aye</strong></p><p>A simple theme with bright colors and comes in three versions <strong>— dark, light, and mirage</strong> for all-day-long comfortable work. My favorite themes.</p><p><strong>2.</strong> <strong>Simple React snippets</strong></p><p>Is used for the essential collection of React snippets and commands</p><p><strong>Snippets shorts</strong></p><p><strong>1.</strong> <strong>Im </strong>Import React</p><p>2. <strong>Imrc </strong>import React / component</p><p><strong>3.</strong> <strong>Impt </strong>import Prop Types</p><p>4. <strong>Cc </strong>class component</p><p><strong>3.</strong> <strong>Prettier — Code Formatter</strong></p><p>Vs code package to format your <strong>JavaScript / Typescript / CSS</strong> using Prettier, My favorite one</p><p><strong>4.</strong> <strong>Material Icon theme</strong></p><p>Material design icons for Visual studio code</p><blockquote><strong><em>To install this extension, we use vs code extension button. On the left side of vs code</em></strong></blockquote><p><strong>At the heart of all React application ore components</strong></p><h3>Component</h3><blockquote>A component is essentially a piece of the user interface. So, when building applications with React, we build a bunch of independent, reusable components and then compose them to build complex user interfaces.</blockquote><p><em>Components allow us to reuse the same structure, and then we can populate those structures with different sets of data</em>.</p><p>The react all applications are components a component is a Piece of the UI.</p><p>Every React app has at least one component, which we refer to as the <strong><em>root component</em></strong>. this component represents the entire application and contains other child components. So, every React application is essentially a tree of components.</p><p><strong>A real example</strong></p><p>Let’s imagine we want to build an application like Twitter. We can split this page into components like <strong>NavBar, Profile, Trends, and Feed.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*mRj3sJ1kTnSnS6t002-R-w.png" /><figcaption>components</figcaption></figure><p>So, on the top, we have <strong>App</strong>, this component has 4 children: <strong>NavBar</strong>, <strong>Profile</strong>, <strong>Trends</strong>, and <strong>Feed</strong>, which includes several <strong>Tweet</strong> components. Each <strong>Tweet</strong> component can include a <strong>Like</strong> component, which we can re-use on the pages or even in different applications.</p><p>Example. Code looks like this</p><p>A component is typically implemented as a <strong>JavaScript class</strong>. That has some <strong>state</strong> and a <strong>render method</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*cmF6IPNzUICBA2FpsrZJgA.png" /><figcaption><strong>render method</strong></figcaption></figure><p><strong>1</strong>- The state here is the data that we want to display when that component is rendered.</p><p><strong>2</strong>- And the render method, as you can tell, is responsible for describing what the UI should look like.</p><p>If a component doesn’t have a state, we can implement it using a pure function instead of a class.</p><h3>Virtual DOM</h3><blockquote>The output of this render method is a React element which is a simple, plain JavaScript object that maps to a DOM element. It’s not a real DOM element, it’s just a plain JS object that represents that DOM element in memory.</blockquote><p>So. React keeps a lightweight representation of the DOM in memory. We refer to this as the virtual DOM. Unlike the browser or the real DOM, this virtual DOM is cheap to create. When we change the state of a component, we get a new React element. React will then compare this element and its children with the previous one, it figures out what is changed, and then it’ll update a part of the real DOM to keep it in sync with the virtual DOM.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/337/1*DdWxg7C-zkczq2ah5Ew_GA.png" /><figcaption>virtual DOM</figcaption></figure><p><em>We simply change the state of our components, and React will automatically update the DOM to match that state.</em></p><h3>React vs Angular 2+</h3><blockquote>React and Angular are similar in terms of their component-based architecture. But Angular is a <strong><em>framework</em></strong> or a complete solution, while React is a <strong><em>library</em></strong>. It only takes care of rendering the view and making sure that the view is in sync with the state. That’s all React does, nothing less, nothing more! For this very reason, it has a small API to learn.</blockquote><p>So, when building applications with React, we need to use other libraries for things like routing or calling HTTP services, and so on. But this is not necessarily a bad thing, because you get to choose the libraries that you prefer as opposed to being fixed with what Angular gives you, which often breaks from one version to another!</p><h3>Application structure</h3><p>Create-react-app gives us everything we need to develop a React application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*UPjRF19BIvRXp6q_V_xRKA.png" /><figcaption>Application structure</figcaption></figure><h3>Meet the React Component</h3><blockquote>Our first React component is in the <strong>src/App.js</strong> file, which should look similar to the example below.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*CDV8wEx4GJCIjQy6RsievQ.png" /><figcaption><strong>App.js component</strong></figcaption></figure><p>first, this React component, called the App component, is just a JavaScript function. It’s commonly called function component, see component types later. Sometimes you need a small, lightweight declaration of a component to just display something simply and easily where you are not worried about interaction. <em>The easiest way to declare a component in React is with functional syntax.</em></p><p><strong>The app component returns code that resembles HTML which is called JSX.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*_jqvBT5ei5xWSQvHESLt6A.png" /><figcaption>JSX</figcaption></figure><h3>Introduction to JSX (JavaScript XML).</h3><blockquote>Recall that I mentioned the returned output of the App component resembles HTML. This output is called JSX. Which mixes HTML and JavaScript.</blockquote><blockquote>To be able to write components in React, we need to understand the primary <strong>templating language that React uses to build a component</strong>. JSX.</blockquote><h3>What is JSX?</h3><blockquote>JSX is the templating language of React. JSX has the benefit that it is just JavaScript. It comes with the full power of JavaScript.</blockquote><p>JSX convert HTML tags into React elements.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*IGOLRg-E6QSepdKPZ01WzA.png" /></figure><h4><em>Let us demonstrate with two examples, the first use JSX and the second does not:</em></h4><p><em>Example 1.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*IuR24zyjOHwcbZUSvUDw7A.png" /><figcaption>JSX</figcaption></figure><p>Example 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Mgjug-Tejd69GHXSOv4JDQ.png" /><figcaption>Without JSX</figcaption></figure><p>As you can see in the first example, using the JSX to descript, what the UI looks like much easier than writing plane react code like this in example 2.</p><p>So, in the component we all use JSX, <strong>Babel</strong> can convert <strong>JSX</strong> syntax! That browser can understand. <strong>Babel</strong> is a modern JavaScript compiler</p><h3><strong>Babel</strong></h3><blockquote>Since we want to use the latest features of JavaScript along with JSX, we’re going to need a way to convert our fancy source code into something that the browser can interpret. This process is called compiling, and its what <strong>Babel</strong> is designed to do.</blockquote><p><strong>Diving deeper into JSX</strong></p><blockquote>- <strong>Expressions in JSX</strong></blockquote><blockquote>with JSX you can write expressions inside <strong>curly braces { }.</strong></blockquote><p>the expression can be a <strong>React variable,</strong> or <strong>property</strong>, or any other valid JavaScript expression. JSX will execute the expression and return the result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*hrs_RsWiCmonPcPIn7n-bw.png" /><figcaption><strong>curly braces { }</strong></figcaption></figure><h3>- <strong>Variables in JSX</strong></h3><blockquote>We’ll talk about how variables are used inside JSX. let’s try making a variable of our own. Before the return statement of an <strong>app</strong> and <strong>const title = ‘React.js’;</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/587/1*Byi1K64UzWJRf0Sfx80ZTA.png" /><figcaption>Variables in JSX</figcaption></figure><h3>- <strong>Specifying Attributes with JSX</strong></h3><blockquote>Does the same thing in React and HTML. as mentioned before, things get trickier with the class attribute in THML. Since the <strong>class</strong> is a reserved word in JavaScript, we instead have to use <strong>className</strong> as a substitute.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*19N1S7qUaJjlbvKk2A9SpQ.png" /><figcaption>Attributes</figcaption></figure><h3>- <strong>Styling HTML elements</strong></h3><blockquote>What if we want to declare styles on a particular HTML element ? this gets a little trickier, since it requires you to write a little bit of JavaScript inside of your element. You will need to embed a JavaScript object inside the style attribute that return whatever CSS properties you need to include, as in the following.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*ANTrUCPVX4gvsYDCI-_8QA.png" /><figcaption>styling THML Element</figcaption></figure><h3>Exploring our first React component — &lt;App/&gt;</h3><blockquote>In React a component is a reusable module that renders a part of our app. These parts can be big or small, but they are usually clearly defined.</blockquote><p>Let’s open <strong>src/App.js</strong> , since our browser is prompting us to end it. this file contains s our first component, App, and a few other lines of code</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*SKkZd7Ek61wQ87WSOLnkdg.png" /><figcaption><strong>src/App.js</strong></figcaption></figure><p><strong>the App.js file consists of three main parts:</strong></p><p>- Import statements at the top</p><p>- The app component in the middle</p><p>- And an export statement at bottom</p><p>The <strong>import</strong> statement at top of the file allow App.js to use code that has been defined elsewhere. These are the modules that are imported</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*3v3DYwFNBSSvE3Acqp5jDg.png" /><figcaption>App.js File</figcaption></figure><p><strong>- the first</strong> statement imports the React library itself. Because React turns the <strong>JSX</strong> we write into <strong>React.createElement()</strong>, <strong><em>all React components must import the React module. If you skip this step, your application will produce an error</em></strong><em>.</em> And it is listed as a dependency in our <strong>package.json</strong> file.</p><p><strong>- The second</strong> statement imports a log form ‘./logo.svg’.</p><ul><li><strong>The third </strong>statement imports the CSS related to our App component. Note that there is no <strong>variable name</strong> no <strong>from</strong> directive. And <strong>— it</strong> comes from <strong>Webpack</strong>, the tool create-react-app uses to bundle all our JavaScript files together and serve them to the browser.</li></ul><h3><strong>The App component</strong></h3><blockquote>After the imports, we have a function named <strong>App</strong>. Most of the JavaScript community prefers camel-case names like helloWord.</blockquote><p>React components use pascal-case variable names, like HelloWord, <strong>to make it clear that a given JSX element is a React component. Not a regular HTML tag.</strong></p><p><em>If we were to rename the App function to app, the browser would show you an error</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*d3VX8LotDULDj1JzGikijA.png" /><figcaption><strong>App component</strong></figcaption></figure><p>The App function returns, a JSX expression, this expression defines what your browser ultimately renders to the DOM.</p><p>Some elements in the expression have attributes, which are written just like in HTML, the <strong>&lt;div&gt;</strong> <strong>tag has a className attribute this is the same as the class attribute in HTML</strong>, <strong>but because JSX is JavaScript, we can’t use the word class</strong></p><h3>Export statements</h3><blockquote>At the very bottom of the <strong>app.js</strong> file, the statement <strong>export default App</strong> makes our <strong>App</strong> component available to other modules.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*IUuUyyowlL_SGaWgXdHt3Q.png" /></figure><blockquote><strong>interrogating the index.js</strong></blockquote><p>let’s open <strong>src/index.js</strong>, because that’s where the <strong>App</strong> component is being used. This file is the <strong>entry point</strong> for our app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*S2jllgnzKAXV9ynOmZ0avA.png" /><figcaption>src/index.js</figcaption></figure><p>- As with App.js, the file starts by importing all the JS modules and other assets it needs to run.</p><p>- <strong>Src/index.css</strong> holds global styles that are applied to our whole app. We can also see our <strong>App</strong> component imported here.</p><p>- It is made available for <strong>import</strong> thanks to the <strong>export statement</strong> at the bottom of <strong>App.js</strong></p><p>- Calls React’s <strong>ReactDOM.render()</strong> function with two arguments</p><p>.The component we want to render<strong>, &lt;App /&gt;</strong> in this case</p><p>.The DOM element inside which we want the component to be rendered, in this case the element with and ID of root. If you look inside <strong>public/index.html</strong>, you’ll see that this is a <strong>&lt;div&gt;</strong> element just inside the<strong> &lt;body&gt;</strong></p><p><strong><em>All of this tells React that we want to render our React application with App component as the root, or first component.</em></strong></p><blockquote><strong>Some Questions in my Mind.</strong></blockquote><p><strong>1-</strong> <strong>How to render HMTL?</strong></p><p><strong>Answer</strong>- to render HMTL we use ReactDOM.render(), React renders HTML to the web page by using a function called. ReactDOM.render().</p><p>The ReactDOM.render() function takes two arguments, HTML code and an HTML element.</p><p>The purpose of the function is to display the specified HTML code inside the specified HTML element.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*TJaBMRHllCsQ-JraVxUbOw.png" /></figure><p>2- <strong>what is render method?</strong></p><p><strong>Answer</strong>- to create class component we should use render method. The component also requires a <strong>render() method</strong>, this method returns HTML.</p><p>3-<strong>what is state?</strong></p><p>In React, component properties should be kept in an object called state.</p><h3>React State</h3><blockquote>React component has a built in state object. The state object is used to store data and property value that belongs to the component.</blockquote><p>When the state object changes the component re-renders.</p><p><em>State is special property in React component and basically it an object that include any data that this component need.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*KaoOQd7Z_K1nytaJDSqc0A.png" /><figcaption>Example: Creating the state Object</figcaption></figure><p><strong><em>we can also use constructor function</em></strong></p><p>The state object is initialized in the constructor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/554/1*awOEDrGuKUqU09lexwHJ7A.png" /><figcaption>state in constructor</figcaption></figure><p><em>Refer to the </em><em>state object anywhere in the component by using the </em><em>this.state.</em>propertyname<em> syntax:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/556/1*-MCTTBZ2fdYNzn1n758dhA.png" /></figure><p><strong>Example 2.</strong></p><blockquote>In this method we want to check the value of the count property. If the value of count property = 0, <strong>count: 0</strong>, we return “<strong>Zero</strong>” as string. Then it will return the value of count itself.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*pENAosMgCEVLOQzu68fpUQ.png" /><figcaption>state</figcaption></figure><p>if the count value change to 0 it Zero as label, or if we change the count value to 1,2, 3 … it will show itself value.</p><h3>Styling</h3><blockquote>there are two primary ways to style react components</blockquote><p>1. Inline styles with the style prop</p><p>2. Regular CSS with the <strong>className prop</strong></p><p><strong>About the style prop:</strong></p><ul><li>In HTML you’d pass a string of CSS</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/577/1*GgMqoFM3SdA0wbiPUaBHgw.png" /><figcaption>html css</figcaption></figure><ul><li>In React you’ll pass an object of CSS:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*v4qxs1OcsyNLJS75hzIeXA.png" /><figcaption>React CSS</figcaption></figure><blockquote><strong>Note </strong>also that the property names are camelCased rather than kebab-cased. The matches the <strong>style</strong> property of DOM nodes (which is a CSSStyleDeclaration object ).</blockquote><p><strong>About the </strong>className <strong>prop:</strong></p><p>As we discussed earlier , in HTML, you apply a class name to an element with class attribute. In JSX, you use the className prop</p><h3>How to install bootstrap</h3><p>- Install the bootstrap using this command. <strong>npm i bootstrap@4.1.1</strong></p><ul><li>Import your bootstrap files to <strong>index.js</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*tPI0H06WoJvai9iIYRPXow.png" /><figcaption>import bootstrap</figcaption></figure><p><strong><em>now we can use bootstrap</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*c6dReyBQDcOjLFUeBrkodg.png" /><figcaption>useing bootstrap</figcaption></figure><h3>Rendering Classes Dynamically</h3><blockquote>Now our badge currently is blue looking the above example, if the <strong>value </strong>of <strong>count </strong>property is <strong>0 zero</strong>, we want to rendering it as <strong>yellow </strong>color, we want to show warning color otherwise <strong>blue</strong>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*Qxfqmi6CJFoUcaKET_FoUA.png" /><figcaption><strong>Rendering Classes Dynamically</strong></figcaption></figure><h3>Reading List</h3><blockquote>We can use <strong>map()</strong> function to render list of an array with this e can render list of items. We loop through the list array using the JavaScript <strong>map()</strong> function . we return <strong>a &lt;li&gt;</strong> element for each item.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*4s85FVb2afH9Sope3OKU8g.png" /><figcaption><strong>Array</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*3DJ6kjBNbCNhmVzSxHiUfw.png" /><figcaption><strong>map function:</strong></figcaption></figure><p><strong>Note</strong>: But when you run this code, you will notice that React also throws a warning. Open your <strong>console</strong> see, this error.</p><blockquote><strong><em>Warning: Each child in a list should have a unique “key” prop.</em></strong></blockquote><blockquote><strong><em>Check the render method of `Counter`. See </em></strong><a href="https://reactjs.org/link/warning-keys"><strong><em>https://reactjs.org/link/warning-keys</em></strong></a><strong><em> for more information</em></strong></blockquote><p>The reason React is because its <strong>need to uniquely identify</strong> each item in this list, because if the state of this React element in the virtual DOM changes React quickly figure out, what element change and where the in DOM it should make changes to keep the DOM, with virtual DOM.</p><p>Because, a “Key” is a special string attribute you need to include when creating list of elements.</p><h3>How do you use Keys in Lists?</h3><p>Keys help React identify which items have changed (added/removed/re-ordered). To give a unique identity to every element inside the array, a key is required.</p><p><strong>To better understand this, let’s refactor the code snippet we saw earlier, to now include keys.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*5sZis8AYLQxDZN5jLnV5iQ.png" /><figcaption><strong>key in list</strong></figcaption></figure><p><strong>But in real Application your key={list} maybe object, they could have id property.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*iuRDPBVQjioSIkqObQZiLw.png" /><figcaption>key list by id</figcaption></figure><h3><strong>Conditional Rendering</strong></h3><blockquote>Conditional rendering is rendering elements that may or may not be displayed on a web page depending on certain conditions or events. These conditionals are decision paths in your code statement. conditional rendering in React works the same way conditions work in JavaScript. We use If operator or the conditional operator to create elements representing the current state, and let React update the UI to match them.</blockquote><p><strong>Example:-</strong></p><p>If our array is empty, our array should have at least one element. Otherwise we want to display a message, there is no data in list,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*P6K4m4AEa8GxjMYEBO7Aag.png" /><figcaption><strong>Conditional Rendering</strong></figcaption></figure><p><em>now our state list is empty it will run the conditions, otherwise it will show our list data.</em></p><p><strong><em>There is also another way to conditioning rendering content. using the </em>&amp;&amp;<em> operator.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*f45-t2Ghc5uhM4m4qqfasg.png" /><figcaption><strong>conditional by &amp;&amp; operator</strong></figcaption></figure><h3>Handling Events In React</h3><blockquote>Handling events with React elements is very similar to handling events on DOM elements. Just like HTML React can perform actions based on user events.</blockquote><p>React has the some events as HTML: <strong>click , change, mouseover</strong> etc .</p><blockquote><strong>Remember Points</strong></blockquote><ul><li>React events are named using <strong>camelCase</strong>, rather than <strong>lowercase</strong> <br> <strong>onClick</strong> instead of <strong>onclick</strong></li><li>With JSX you pass a function as the event handler, rather than a string. <br> React event handlers are written inside curly braces <br> <strong>onClick={ handleIncrement() } </strong>instead of<strong> onClick=” handleIncrement”</strong></li><li>A good practice is put the event handler as a method in the component class.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*KLhPiGgQS9KR9DgpdCk_Yw.png" /><figcaption><strong>event handler</strong></figcaption></figure><p>When we click on the button the function will be run in console.</p><h3>Binding Event handler this keyword</h3><blockquote>For methods in React, <strong>this keyword</strong> should represent the component that owns the method. If we use this keyword in <strong>handleIncrement()</strong> function it won’t working. because <strong>this keyword</strong> in JavaScript behaves differently from other languages</blockquote><p><strong>What is the problem solution?</strong></p><p><strong>There is two ways.</strong></p><ul><li>We use the <strong>bind() method</strong> to solve this problem</li><li>We use the <strong>arrow function</strong> to solve this problem</li></ul><p>Both of them are good but good practice is to use the arrow function.</p><p>That is why you should use <strong>arrow functions</strong>, with arrow functions, <strong>this keyword </strong>will always represent the object that defined the arrow function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*RtTGzDfvlJl5mmg1KDAsIA.png" /></figure><h3>Updating the state</h3><blockquote>To change a value I the state object, use the <strong>this.setState()</strong> method. React knows the state has changed, and calls the render() method again to learn what should be on the screen.</blockquote><p>When a value in the state object changes, the component will re-render, meaning that the output will change according to the new values.</p><p><strong>Example</strong>:</p><p>We add button with an <strong>onClick event</strong> that will change the value of <strong>count property. To click the button it will increment the value.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*heNjMGizL8FWlR804R0eTg.png" /><figcaption><strong>Updating the state</strong></figcaption></figure><blockquote><strong>Do not modify state directly</strong></blockquote><p>For example, this will not re-render a component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*w2OYqk830LLQhGq1XX_R0w.png" /></figure><p>instead, use <strong>setState()</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*YwELi75luCGFKc-h8L8Gug.png" /></figure><h3><strong>what happens when state changes.</strong></h3><p>let’s see what exactly happen when we click the Increment button, we are calling <strong>this.setState()</strong> <strong>method</strong>, this method telling to React that the state of the component is going to change, React will then schedule a call to the <strong>render() method</strong>, some time in the future this method is going to be call, we don’t know when. This is a asynchronous call, which mean going to happen in the future, it may happen 5 millisecond later, or 10 millisecond later, we don’t know. So at some point in future the <strong>render() method</strong> is going to be call.</p><p>And this <strong>render() method</strong> is return new React element, element is this <strong>&lt;div&gt;</strong> at top this React element has two children a <strong>&lt;span&gt;</strong> and the <strong>&lt;Button&gt;</strong> .</p><p>So our <strong>virtual DOM</strong> is tree of three elements the React element on the top that is <strong>&lt;div&gt;</strong> two children &lt;<strong>span&gt; &lt;button&gt;,</strong> here we have <strong>New Tree Virtual DOM</strong>, we also have <strong>Old Tree Virtual</strong> <strong>DOM</strong></p><p>React will put these side by side and compare them to figure out, what elements in the <strong>virtual DOM</strong> are modifying</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*6YDFB4G_Svv8wmeemfW8uQ.png" /><figcaption>virtual DOM</figcaption></figure><h3>React Hooks</h3><p>How to use state in React Hooks?</p><blockquote>Starting with version 16.8.0, React introduced hooks. And they’ve completely changed the way we write code in React.</blockquote><p><em>Hooks</em> are a new addition in React 16.8. They let you use state and other React features without writing a class.</p><p>Using React Hooks we can use state and lifecycle methods inside functional components.</p><p>React hooks are functional components with added <strong>state</strong> and <strong>lifecycle</strong> methods.</p><p>we are going to introduce two of the popularity used React hooks: <strong>useState</strong> and <strong>useEffect</strong> . these are widely used and can solve most of our problems.</p><p><strong><em>Note:</em></strong><em> there are other types of hooks that come bundled with React library. We can check React website online documentation</em></p><p><strong>useState Hooks</strong></p><blockquote>this is new function <strong>useState</strong> in the first “Hook” Starting React by learning about the useState hook to manage component state.</blockquote><p>However, I have recently discovered that it possible to use a state object instead.</p><h3><strong>Question!</strong></h3><p><strong>-</strong> What is deference between <strong>state</strong> and <strong>useState</strong>?</p><p>I am confused, and can’t seem to find many references on the internet to help me understand the difference.</p><p><strong>Option 1. State Object</strong></p><p>State object In previous we studied state object, you can check it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*FxqLHJwWPRb_BaJZp5oSdg.png" /><figcaption><strong>state Object</strong></figcaption></figure><p><strong>Option 2. useState hook.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*LXsi55lu96LVQlh78z5hKg.png" /><figcaption><strong>useState hook</strong></figcaption></figure><p><em>They’re not interchangeable.</em></p><blockquote><strong>1-</strong> <strong>this.state:</strong></blockquote><p><strong>is use for class component.</strong></p><p>State object, or <strong>this.state</strong> was used when React used class</p><blockquote><strong>2-</strong> <strong>useState:</strong></blockquote><p><strong>is use for function component.</strong></p><p>Before <strong>React 16</strong>, function could be used as component, but didn’t have state, with Hooks, the functions could have state as well.</p><p>It’s means that <strong>state</strong> are used in class component, and <strong>useState</strong> are used in function component.</p><blockquote><strong>Equivalent class example</strong></blockquote><blockquote>If you used classes in React Before, this code should look familiar.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*PmVGwaJGiQEwE7j3YJAKqg.png" /><figcaption>without Hook</figcaption></figure><p><strong>We’ll start learning about Hooks by comparing this code to an equivalent class example.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*L9fh5T_bff5FKqdPTSyV-g.png" /><figcaption>with Hook</figcaption></figure><blockquote><strong>When would I use a Hook?</strong></blockquote><blockquote>If you write a function component and realize you need to add some state to it, previously you had to convert it to a class. Now you can use Hook inside the existing function component.</blockquote><p><strong>Declaring a State variables</strong></p><p><strong><em>We import the </em></strong><em>useState</em><strong><em> Hook from React. It lets us keep local state in a function component.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*jjmqLMpd-oGxW7kqmlZOZg.png" /><figcaption><strong>Declaring a State variables</strong></figcaption></figure><p>we declare a state variable called <strong>count,</strong> and set it to<strong> 0</strong>. React will remember its current value between re-renders, and provide the most recent one to our function.</p><p>If we want to update the current count, we can call <strong>setCount</strong>.</p><h3>Reading state</h3><blockquote>When we want to display the current count in a class, we read <strong>this.state.count</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*qSOc03F4hNqDKzYOvdjZBw.png" /></figure><p><strong>But in a function, we can use count directly.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*PwFVg6cbJDuwo0ylLioZDQ.png" /></figure><blockquote><strong>Updating State</strong></blockquote><blockquote>In class, we need to call <strong>this.setState()</strong> to update the <strong>connt</strong> state</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*zi91f1JOPql3txNF4s27Og.png" /></figure><p>in a function, we already have <strong>setCount</strong> and <strong>count</strong> as variables so we don’t need <strong>this</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*qpPYFIo5g-99pW36cYEI_A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*3wwbYsDB_CWh09gefAD34A.png" /><figcaption><strong>Full Code</strong></figcaption></figure><h3>Props</h3><p><strong>What are props in React?</strong></p><blockquote>Another important concept of components I show they communicate. React has a special object called a prop (stands for property) which we use transport data from one component to another.</blockquote><p>Passing in props allows components to take in information along the way and change how the component is rendered to the end user</p><p>But be careful props only transport data in a <strong>one-way flow</strong> (<strong>only from parent to child components</strong>) it is no passible with props to pass data form child to parent.</p><blockquote><strong>Example:</strong></blockquote><blockquote>Let’s revisit the <strong>ParentComponent.jsx</strong> page to see how to pass data with props.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*2oz2kxYKZswwyOtp_28RJg.png" /><figcaption><strong>ParentComponet.jsx</strong></figcaption></figure><ul><li>first, we define a prop on the <strong>ParentComponent </strong>Component and assign a value to it.<strong> [</strong> <strong>&lt;ChildComponent name=”First Child” /&gt; ]</strong></li></ul><blockquote>props are custom values and they also make components more dynamic. Since the (<strong><em>ChildComponent</em></strong>), component is the child here, we need to define props on its parent (<strong><em>ParentComponent</em></strong> ), so we can pass the values and get the result simply by accessing the prop “name”:</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*PbOIKM6zuh6a2scaRfUshw.png" /><figcaption><strong>childComponent.jsx</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*cEcwGAn0Lw1Vjky9HAlFtg.png" /><figcaption><strong>Full Code</strong></figcaption></figure><h3>Props vs state</h3><p>What are differences between Props and state</p><ul><li>Props include data that we gave to a component,</li><li>or state include data that is local or private to that component,</li><li>so others component not access that state, it’s completely internal to that component.</li><li>Props are used to pass data, whereas state is for managing data</li><li>Data from props is read-only, and cannot be modified by a component that is receiving it from outside.</li></ul><p>All the attributes that we set these are the props the input to the component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*XJX_Ehna0J_TXPD6Rzd_zg.png" /></figure><p>State is not to be confused with props. State is what is managed within the component, whereas props is what get passed to the component.</p><blockquote><strong>Example 2:</strong></blockquote><blockquote><strong>Creating Profile Component as a Class Component</strong></blockquote><p>We have 2 files <strong>profile.jsx</strong> and <strong>App.jsx</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*45SLj3jWVtQfondcmZK2mA.png" /><figcaption><strong>App.jsx</strong></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*oOxQ0ZnE7GfwcENL6o8o7w.png" /><figcaption><strong>profile.jsx</strong></figcaption></figure><blockquote><strong>Example 3:</strong></blockquote><blockquote>we’ll use React props to pass the array to the <strong>List</strong> component:</blockquote><p>we need 2 files <strong>App.jsx</strong> and <strong>list.jsx</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*Z-jkwg6jUDQdal6UVQQVwg.png" /><figcaption><strong>App.jsx</strong></figcaption></figure><p>the variable is called <strong>stories</strong> in the <strong>App component</strong>, and we pass it under this name to the <strong>List component</strong>. In the List component’s instantiation, however, it is assigned to the <strong>list attribute</strong>. We access it as list from the props object in the List component’s function signature.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*YLxTmb_1LoIxKrYypyeqKg.png" /><figcaption><strong>list.jsx</strong></figcaption></figure><p>using this operation, we’ve prevented the <strong>list/stories variable</strong> from polluting the global scope in the <strong>App component</strong>. Since stories is not used in the App component directly, but in one of its child components, we passed them as props to the List component, there, we can access it through the first function signature’s argument, <strong>called props</strong>.</p><h3>Composing components</h3><blockquote>Components can <strong>refer t</strong>o other components in their output. This lets us use the same component abstraction for any level of detail. A button, a form, a dialog, a screen. In React apps, all those are commonly expressed as components.</blockquote><p><strong>Or</strong></p><blockquote>So for this application we have work with the single component, the React application is tree of components, so we can compose components together to build complex user interfaces.</blockquote><p><strong>For example</strong>, we can create an <strong><em>App</em> component</strong> that <strong>renders <em>Counter</em> many times</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*D1ks6LYdnCopnrteAvrYhA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*wIu2jrfaZOtzb8uoF2TWeQ.png" /><figcaption><strong>App.jsx</strong></figcaption></figure><p><strong>React.js is one today’s of the most widely used JavaScript libraries that every front-end developer should know.</strong></p><blockquote>I hope this article helps you understand the basic React, concept, there are also other important things to cover about React, and I will keep writing about them later.</blockquote><p><strong>Thank you for reading :)!</strong></p><p><strong>Good luck!</strong></p><p><strong>Said Shah Ahmadi</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18ad91cbd761" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[In order to create React.js applications we should know JavaScript (ES6)]]></title>
            <link>https://saidshahahmadi.medium.com/in-order-to-create-react-js-applications-we-should-know-javascript-es6-5af41ed1af85?source=rss-ab713604c088------2</link>
            <guid isPermaLink="false">https://medium.com/p/5af41ed1af85</guid>
            <dc:creator><![CDATA[saidshah Ahmadi]]></dc:creator>
            <pubDate>Sat, 22 May 2021 18:36:52 GMT</pubDate>
            <atom:updated>2021-05-22T19:53:37.165Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Modern JavaScript (ES6)</strong></p><p>In order to build applications in React.js, we should know Modern JavaScript (ES6)</p><p><strong>Modern JavaScript essentials.</strong></p><p>· Var, let, const</p><p>· Objects</p><p>· This</p><p>· Arrow function</p><p>· Destructing</p><p>· Spread</p><p>· Classes</p><p>· Modules</p><blockquote><strong>Let, var and const</strong></blockquote><blockquote>A variable is a “<strong>named storage</strong>” for data. We can use variables to store goodies, visitors, and other data.</blockquote><p>To create a variable in JavaScript, use the <strong>let</strong> keyword.</p><p><strong>Let message</strong></p><p><strong>Let message = ‘Hello’;</strong></p><p><strong>Var instated of let</strong></p><p>In the older script, you may also find. Another keyword, <strong>var</strong> instead of <strong>let</strong></p><p><strong>Var message ‘hello’;</strong></p><p>The <strong>var</strong> keyword is almost the same as <strong>let</strong>. It also declares a variable, but in a lightly different <strong>“old school way “. it’s</strong> generally not used in modern scripts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*K8HhgnvkwTXgY7uGyzvdRQ.png" /></figure><p>now the <strong>var</strong> variable is accessible inside of the <strong>Function</strong> in which it’s defined.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*mEbBA7xubYrvIFvDn0xRSQ.png" /></figure><p>now we declare the variable with <strong>let keyword</strong> that variable only accessible inside that block. The second <strong>console.log(i)</strong> will give an error. Because <strong>let</strong> is block scope.</p><p><strong>Notes: Var</strong> -&gt; variable declares with <strong>var keyword</strong> are scoped to the function.<strong>(var -&gt; function)</strong></p><p><strong>Let</strong> -&gt; variable declares with <strong>let keyword</strong> are scoped to the block in which that defined <strong>(let-&gt; block) .</strong></p><p><strong><em>Var doesn’t have block scope but function scope</em></strong></p><p>Let is a new feature introduced I ES205 and it’s essentially a block-scoped version of <strong>var</strong>. Modern JavaScript developers might choose to only use <strong>let</strong> and completely discard the use of <strong>var</strong>.</p><blockquote><strong>Using constants (const)</strong></blockquote><p>To declare a constant (unchanging) variable, use <strong>const </strong>instead of <strong>let</strong>.<br> <strong>const myBirthday = ’12.06.1998’;</strong></p><p>Variable declared with <strong>var</strong> or <strong>let</strong> can be changed later. But Variables declared using <strong>const</strong> are called “constants”. They cannot be changed.</p><p><strong>const myBirthday = ’12.06.1998’;</strong></p><p><strong>myBirthday=’3.5.2021’;</strong></p><p>when a programmer is sure that a variable will never change, they can declare it with <strong>const</strong> to guarantee and clearly communicate that fact to everyone.</p><blockquote><strong>object</strong></blockquote><blockquote>objects are used to store keyed collection of various data and more complex entities.</blockquote><blockquote>An object literal is a collection of name-value pairs.</blockquote><p>In JavaScript, most things are objects, from core JavaScript features like arrays to the browser APIs. Built on top of JavaScript. we can create our own object to encapsulate related functions and variables into an efficient package.</p><p>An object which usually consists of several variables and function which are called <strong>Properties</strong> and <strong>Method</strong>, when they are inside objects</p><p><strong>Variables</strong> -&gt; properties <br> <strong>Function</strong> -&gt; Methods</p><p><strong>Creating an objects</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*1-CzsXsCLFRzuBLMmto3Mw.png" /><figcaption>Creating an objects</figcaption></figure><blockquote><strong>Literals and properties</strong></blockquote><p>We can immediately put some properties into {. . .} as “key: value” pairs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*HAWyuvw_6s80BzvBUZ6tKQ.png" /></figure><p>property has a key (also known as <strong>“name”</strong> or <strong>“identifier”</strong>) before the colon “:” and a value to the right of it.</p><blockquote><strong>In the person object, there are two properties:</strong></blockquote><p>1. The first <strong>property</strong> has the name <strong>“name”</strong> and the value <strong>“said”</strong></p><p>2. The second one has the name <strong>“age”</strong> and the value <strong>22</strong>.</p><p>Property values are accessible using the <strong>dot </strong>notation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/730/1*qXi_W4fsNA_iV0FUbPrs8A.png" /></figure><p>To remove a property, we can use the <strong>delete</strong> operator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*ps8yZcVOzLrSI3GbM4F_fA.png" /><figcaption>remove a property</figcaption></figure><blockquote><strong>using a function in an object.</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/738/1*fL1hmVE80ZcXw2IsBggWFA.png" /></figure><blockquote><strong>The this keyword</strong></blockquote><p>in other words, <strong>this. name</strong> means the <strong>name property</strong> of these <strong>objects</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*5hDfaIjpQ6WcGIKiv6zLNw.png" /></figure><blockquote><strong>bind method</strong></blockquote><p>In JavaScript function are object, we use bind () method to bind a function to an object.</p><p>Functions provide a built-in method bind that allows to fix this.</p><p><strong>The basic syntax is:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*rv7UiL34dlrNqs65_8i7sw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*ba7wjeC-1Kf3me2rw9m2Wg.png" /></figure><blockquote><strong>Function in JavaScript</strong></blockquote><blockquote>Often we need to perform a similar action in many places of the script.<br> for example. We need to show a nice-looking message when a visitor logs in, logs out, and maybe somewhere else.</blockquote><p>Functions are the main <strong>“building blocks”</strong> of the program. They allow the code to be called <strong>many times without repetition</strong>.</p><p>We’ve already seen examples of <strong>built-in functions</strong>. Like <strong>alert(message), prompt (message, default) </strong>and <strong>confirm(question</strong>).</p><p>But we can create a function of our own as well.</p><p><strong>It looks like this:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*b3Qv4jv9wCCmJ4s-YiAEGg.png" /></figure><blockquote><strong><em>One of my favorite features of modern (ES6) JavaScript is arrow functions.</em></strong></blockquote><p>The above function is an old JavaScript function in ES6 we have a cleaner way to add this code to the function.</p><blockquote><strong>Arrow Function</strong></blockquote><blockquote>There’s one more very simple and concise syntax for creating functions. That’s often better than functions. It’s called <strong>“arrow function”,</strong> because it looks like this.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*oAerqHjDXt9P9Uao2AkJpA.png" /><figcaption><strong>Arrow Function</strong></figcaption></figure><p><strong>if we have only one argument, then parentheses can be omitted (remove) making that even shorter.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*LI0BIpEC5Lt3I-GQED3zqQ.png" /></figure><p><strong>if there are no arguments, parentheses should be empty (but they should be present).</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*AhyA65P1nbcEHnB2dHFFVA.png" /></figure><p>an arrow function can be used in the same way as Function Expressions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*76C9NmY7fihNW9zbaiN47g.png" /><figcaption>function expressions</figcaption></figure><p><strong>arrow functions may appear unfamiliar and not very readable at first, but that quickly changes as the easy get used to the structure.</strong></p><p><strong>They are very convenient for simple one-line actions when we’re just too lazy to write many words.</strong></p><p>Let me show you where these <strong>arrow functions</strong> are really useful, let’s imagine, we have an array of jobs. Each job object has an <strong>ID</strong>, and a property called <strong>ACTIVE</strong> which we set <strong><em>true</em></strong> or <strong><em>false</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*GMkgHnIu3Chmf8rff2BQ5A.png" /></figure><p>Now let’s imagine we want to get only the active jobs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*QpwKJcgSlHscdbrPl3Cpjg.png" /></figure><p><strong>this is old styles function syntax.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*O7IVSzS2TUC_bet63tJhJQ.png" /></figure><blockquote><strong><em>note:- Arrow function does not have its own bindings to this keyword</em></strong></blockquote><p><strong>Differences &amp; Limitations:</strong></p><ul><li>Does not have its own bindings to <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this">this</a> or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super">super</a>, and should not be used as <a href="https://developer.mozilla.org/en-US/docs/Glossary/Method">methods</a>.</li><li>Does not have <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a>, or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target">new. target</a> keywords.</li><li>Not suitable for the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call">call</a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply">apply</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind">bind</a> methods, which generally rely on establishing a <a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope">scope</a>.</li><li>Can not be used as <a href="https://developer.mozilla.org/en-US/docs/Glossary/Constructor">constructors</a>.</li><li>Can not use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield</a>, within its body.</li></ul><blockquote><strong>Naming a function</strong></blockquote><p>· Function is actions. So their name is <strong>usually a verb.</strong></p><p>· It should be brief, as accurate as possible, and describe what so that some reading the <strong>code gets an indication of what the function does</strong></p><p>It is a widespread practice to start a function with a verbal prefix that vaguely describes the action. <strong>there must be an agreement within the team on the meaning of the prefixes</strong>.</p><blockquote><strong>Function starting with ….</strong></blockquote><p>· get…. — return a value,</p><p>· calc… — calculate something,</p><p>· create… — create something,</p><p>· check… — check something and return a Boolean, etc.</p><blockquote><strong>examples of such names</strong></blockquote><p>· <strong>showMessage(…)</strong> // shows a message</p><p>· <strong>getAge(..)</strong> // returns the age (gets it somehow)</p><p>· <strong>calsSum(..)</strong> // calculates a sum and returns the result</p><p>· <strong>createForm(..) </strong>// creates a form (and usually returns it)</p><p>· <strong>checkPermission(..)</strong> // checks a permission, returns true/false</p><p><strong><em>with prefixes in place, a glance at a function name gives an understanding of what kind of work it does and what kind of value it returns.</em></strong></p><blockquote><strong>One function — one action</strong></blockquote><blockquote>A function should do exactly what is suggested by its name, no more.</blockquote><blockquote><strong>Array.map()</strong></blockquote><blockquote>The map () method creates a new array with the result of calling a function for every array element.</blockquote><p>The map() method calls the provided function once for each element in an array, in order.</p><p>It’s useful when calling a function that returns an array in the map() callback.</p><p><strong>Mosh. Es5 introduces a new method in arrays called map() this very useful method and in react we use it to render list. When we render a list of items, that’s we use the map() method of the array.</strong></p><p><strong>Example.</strong></p><p>So Let’s say we have an array of colors <strong>with three 3 items</strong>. <strong>[red, green, blue]</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*eA3p_Lvknebt8IXk06XoUw.png" /></figure><p>we call the map () method on this array colors. map() and here we need to pass a callback function. The job of this function is to transform each element in this array. It takes one item at a time and returns a new item.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*rQ8vsfIOuEo4dBc-haZnVw.png" /></figure><blockquote><strong>object Destructing</strong></blockquote><blockquote>the destructing assignment also works with objects.</blockquote><p><strong>Syntax.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*q2suDQkCkoZhBu5ojGBkvQ.png" /></figure><p><strong>Example.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*-rx0b5dp8dqiJ9lbbrsGsw.png" /></figure><p><strong>Old way</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*RRSKOEd7D3X93A7RqZu-cw.png" /></figure><blockquote><strong>Changing the Name of the property</strong></blockquote><p>If we want to assign a property to a variable <strong>with another name, </strong>for instance, <strong>address. street</strong> to go into the <strong>variable named st</strong>, <strong>then we can set it using a colon:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*9tfZuCuf6UXnJItEhlnsfA.png" /></figure><p><strong><em>The colon shows “what: goes where”.</em></strong></p><blockquote><strong>Spread operator</strong></blockquote><blockquote>Es6 provides a new operator called spread operator that consists <strong>of three dots (…)</strong>. the spread operator allows you to spread out elements of an iterable objects such as an <strong>array</strong>, a <strong>map</strong>, or a <strong>set</strong>.</blockquote><p>The spread syntax can be used when all elements from an object or array need to be included in the list of some kind.</p><p>Example</p><p><strong>we can even combine the spread operator with normal values.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*W3MPCJP2DoCO3oddeCiFnA.png" /></figure><p><strong>we can also apply the spread operator to an object.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*p5nR4wa_pL4YLVTOOXsqdw.png" /></figure><p><strong>or</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*SUWBZQP-C1LJhRo9jnZWDQ.png" /></figure><blockquote><strong>Classes in JavaScript</strong></blockquote><blockquote>A JavaScript class is a <strong>blueprint for creating objects</strong>. A class encapsulates data and functions that manipulate the data.</blockquote><p><em>In JavaScript classes are functions. ES6 classes are just a special function</em>.</p><p>To verify the fact that classes are special functions, you can use the <strong>typeof</strong> operator to check the type of the <strong>Person</strong> class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*2tl7mY2O1Sjh2wcMqMDY8g.png" /></figure><p><em>if we often need to create many objects of the same kind, like users, or goods or whatever.</em></p><p><strong>The “class” syntax</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Vl12jBiz1XcphyfLq36p2w.png" /></figure><p>Then new MyClass () creates a new object with all the listed methods. The constructor() method is called automatically by new, so we can initialize the object.</p><p><strong>Example.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*2V9Oe5QhgU52a9xGe9GCVw.png" /></figure><p>When a <strong>new Person(“Said”)</strong> is called:</p><p>1. A new object is created.</p><p>2. The constructor runs with the given argument and assigns <strong>this.name</strong> to it.</p><p><em>… Then we can call methods, such as </em><strong><em>person.walk() and person.name</em></strong><em>.</em></p><blockquote><strong>Why we use Class</strong></blockquote><blockquote><strong>Mosh Hamedani.</strong><br> look at this code we use this <strong>object Person</strong> with two members <strong>name</strong> and <strong>walk ().</strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*pYwSEjtl1Wy0JPyiSQxW9Q.png" /></figure><p>if we want to create another Person object, that can walk, so we should duplicate the code and called this object Person2. There is a problem the problem is we have to duplicate the implementation of the <strong>walk()</strong> method, so we can use Class to solve this problem.</p><blockquote><strong>Inheritance</strong></blockquote><blockquote>Es6 supports the concept of Inheritance. inheritance is the ability of a program to create new entities from an existing entity.</blockquote><p>The class that is extended to create newer classes is called the <strong>Parent class / super class. </strong>The newly created classes are called the <strong>child/sub classes.</strong></p><p>A class inherits from another class using the “<strong>extends</strong>” keyword.</p><p>The child classes inherit all <strong>properties</strong> and <strong>methods</strong> except <strong>constructors</strong> from <strong>the parent class.</strong></p><p>A class can extend another class, and objects initialized using that class inherit all the methods of both classes.</p><p><strong>Syntax</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*ovZLy9c_wHXRSzlAoM-Cog.png" /></figure><p><strong>Example</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*eS3y8FvAh0cYJVEiqzz_gQ.png" /></figure><p>Let’s said we want to define <strong>Teacher</strong> class</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*w63HNwYVsv3_0UwoOxCiGA.png" /></figure><p>However, all teachers should be able to walk, because they all Persons. We don’t want to duplicate this walk() method in the Person, and Teacher classes, so how can solve these problems. Use the inheritance.</p><p>The Teacher class inherits from the Person class. It means it will inherit all methods defined in this Person class<strong>. We add keyword extends, so Teacher extends Person</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*1lAxiWz4tXQqDs0X4Ki5Pg.png" /></figure><p><strong>Now if we create a Teacher object,</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*YQdBSTpDKjWqOxXYvCkdUQ.png" /></figure><p>we have walk method which we have inherit from the Person Class. And the teach() method that added in Teacher Class.</p><blockquote><strong><em>This is inheritance</em></strong></blockquote><p>Now let’s take to the next level</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*b_X3Dja-Bg5YK_kKnRjfGA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*2c17sk8UzTxOIAg3dp1DLw.png" /><figcaption><strong><em>inheritance</em></strong></figcaption></figure><blockquote><strong><em>this part in the code.</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*QuwSIGqIXuyZDW5pWT4AtQ.png" /></figure><p>Whenever we add a <strong>constructor</strong> in the child class, we need to call the constructor of its parent class, here in the constructor we special keyword <strong><em>super</em>(), </strong>which references the parent class. We call it just like method.</p><p>· <em>Super() method to call a parent method.</em></p><p>· <em>Super() to call a parent constructor (inside our constructor only)</em></p><blockquote><strong>Modules</strong></blockquote><blockquote><strong>Modules, introduction</strong></blockquote><blockquote>As our application grows bigger, we want to split it into multiple files, so-called ‘<strong>modules’</strong> A module usually contains a <strong>class</strong> or a <strong>library</strong> of useful <strong>functions</strong>.</blockquote><p><strong>What is a module?</strong></p><p>A module is just a file, a single script, as simple as that.</p><p>There are directives <strong>export</strong> and <strong>import</strong> to interchange functionality between modules, call functions of one module form another one.</p><p>· <strong>Export</strong> — keyword labels variables and functions that should be accessible from outside the current module.</p><p>· <strong>Import</strong> — allows importing functionality from other modules.</p><p><strong>More about Modules in JavaScript</strong></p><p>For a long time, JavaScript existed without a language-level module syntax. That wasn’t a problem, because initially, scripts were small and simple, so there was no need.</p><p>But eventually, scripts become more and more complex, so the community invented a variety of ways to organize code into modules, special libraries to load modules on demand.</p><p>For instance:</p><p>· <strong>AMD</strong> — one of the most ancient module systems, initially implemented by the library require.js.</p><p>· <strong>CommonJS</strong> — the module system created for Node.js server</p><p>· <strong>UMD</strong> — one more module system, suggested as a universal one, compatible with AMD and CommonJS.</p><p>Now all these slowly become a part of history, but we still can find them in old scripts. The language-level module system appeared in the standard in 2015, gradually evolved since then, and is now supported by all major browsers and in Node.js.</p><p>We have multiple classes that define in some files. It will be much nicer if split this code accrues multiple files, this what we call Modularity. Instead of writing all code in one file, we write our code in multiple files, we call each file a module.</p><p><strong>Our code without modularity</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*f2ZLAjQrx5EbOdTX_X7s9A.png" /></figure><p><strong>let’s go modularizes our application</strong></p><p><strong><em>I am going to move each file into a spirited file.</em></strong></p><p><strong>Step 1. Create a file person.js file</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*OmUlsq89XYF7wK02tUd7WQ.png" /></figure><p><strong>Step 2. Create a file teacher.js file</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*OGi70_JlsFBGF_G8e3f5GA.png" /></figure><p><strong>Step 3 . to execute both files in the index.js file.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*EHR34p5OI-FicSuN72-OCw.png" /></figure><p><strong>Step 3. To Execute the modules using an HTML file</strong></p><p>To execute both the modules we need to make a HTML file. Note we should use the <strong>attribute type =” module”</strong> in the script tag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*WRMoetG35s9mDUT1WLwvMA.png" /></figure><p>We spirited our code, now we can see each, we have less code in each file, our application is more maintainable.</p><blockquote><strong>Importing a Module</strong></blockquote><blockquote>To be able to consume a module, use the import keyword. A module can have multiple import statements.</blockquote><p><strong>1.</strong> <strong>Importing Named Exports</strong></p><p>While importing named exports, the names of the corresponding components must match.</p><p><strong>Syntax</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Mn9rhzjAfDua1wp4LRvTLA.png" /></figure><p><strong>2.</strong> <strong>Import “as”</strong></p><p>However, while importing named exports, they can be renamed using the as a keyword.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*5Xz42j8XYeVhstw7K6Oxxw.png" /></figure><p><strong>3.</strong> <strong>Import *</strong></p><p>If the list is long, we can import everything as an object using <strong>import * as &lt;obje&gt;</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*_vi2EA449ShBDCnPsbScbw.png" /></figure><p><strong>4.</strong> <strong>Importing default exports</strong></p><p>Unlike named exports, a default export can be imported with any name.</p><p>A module can have one and only one default export. The default export is easier to import. The default for a module can be a variable, a function, or a class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*CYyZ0HwGatMqBcdtB9upaA.png" /></figure><p><strong>let me quick recipe</strong></p><p><strong>with default export</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*ldpo5xoD1GwARj-68nc41Q.png" /></figure><p><strong>with name export</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*W5hZsbu8pvutcacZnG8CsQ.png" /></figure><h3>Best of luck. Enjoy :)</h3><h3>Said Shah Ahmadi</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5af41ed1af85" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>