<?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 samira saad on Medium]]></title>
        <description><![CDATA[Stories by samira saad on Medium]]></description>
        <link>https://medium.com/@samirasaad577?source=rss-4531a331b66d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*yraLvmCtVPgM3NQqoK7Wgg.jpeg</url>
            <title>Stories by samira saad on Medium</title>
            <link>https://medium.com/@samirasaad577?source=rss-4531a331b66d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 23:05:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@samirasaad577/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[How To Make Light/Dark Mode Theme For Your Website Using SCSS]]></title>
            <link>https://medium.com/@samirasaad577/how-to-make-light-dark-mode-theme-for-your-website-using-scss-2cade0107a9?source=rss-4531a331b66d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2cade0107a9</guid>
            <category><![CDATA[html]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[scss]]></category>
            <category><![CDATA[light-mode]]></category>
            <category><![CDATA[dark-mode]]></category>
            <dc:creator><![CDATA[samira saad]]></dc:creator>
            <pubDate>Tue, 15 Dec 2020 20:43:18 GMT</pubDate>
            <atom:updated>2020-12-15T20:43:18.974Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IHZsXIeA-iBw5JV8sk6aXA.jpeg" /></figure><h3><strong>Dark mode is the latest tech design trend</strong></h3><p>It is the most of people`s display preference</p><p>So, in this article we`ll learn how to make our website more trendy , supporting light and dark mode</p><p><strong>there are several solutions to do that such as,</strong></p><p>1- making two separate stylesheets, one for each theme</p><p>2- using styled component if you are using react js</p><p>3- using scss fearures like maps and mixins</p><p>All of them will get you what you want but,</p><p>the first solution not recommended as there will be too much redundant of code</p><p>the second one is good but what if you used another framework ?!! should i change it all ??!</p><p>So, i personally recommend the last solution, to make use of scss features.</p><p><strong>Live Demo :</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fjolly-moon-wwijo%3Ffile%3D%2Fscss%2F_variables.scss%3A184-197&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fjolly-moon-wwijo%3Ffile%3D%2Fscss%2F_variables.scss%3A184-197&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fwwijo%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f7119a63df808777b60ae5884bc4655/href">https://medium.com/media/0f7119a63df808777b60ae5884bc4655/href</a></iframe><h3><strong>Explanation :</strong></h3><p>We have a simple html page has a toggling button to toggle between light and dark mode and some text</p><p>with each toggling action, the button text should be changed to the another mode text and the page theme also should be replaced with the desired theme colors</p><p>in the scss directory we have scss partials one for variables and one for theme, they are imported inside styles.scss which is the main file that imported into index.js and its the one will be compiled into css after all</p><ul><li>variables partial contains <strong>colors variables</strong></li><li>theme partial contains <strong>mixin and map</strong></li></ul><blockquote><a href="https://sass-lang.com/documentation/values/maps"><strong>map</strong></a>: its a scss feature that returns a key/value pair like <strong>objects</strong></blockquote><blockquote>Scss maps are immutable (they cannot change). So, the map functions will return a new map, and not change the original map.</blockquote><p>the theme partial holds a map called <strong>$themes.</strong></p><p>consists of two maps one for dark-mode and one for light-mode .</p><p>$themes mixin iterates over all themes and returns a selector for each mode <strong>.#{$theme}</strong></p><blockquote><strong>.#{$theme}</strong> =&gt; .light-mode or .dark-mode</blockquote><blockquote>it will be the selector that is a wrapper for the selected mode styles</blockquote><p><strong>theme-get :</strong> Its a helper function takes the key and returns the value from the mode map that represents that key, <strong>key name inside each mode map , like text-color</strong></p><p><strong>Ex</strong> : calling theme-get function</p><p>theme-get(“text-color”) → map-get(light-mode, text-color) →$textColorLight →#ffffff</p><p><strong>Here</strong> we`re done with the styles.</p><p>Inside html.js the toggling button should toggle light-mode and dark-mode class on the parent element which is html element here</p><blockquote><strong>Note</strong>:</blockquote><blockquote>dark-mode and light-mode in map are the same names of the class selector .</blockquote><blockquote>i used light-mode as default</blockquote><h3><strong>And here you got your light/dark theme done</strong></h3><p><strong>Important note:</strong></p><p>inside a real large project your partials should be separated mixins, maps, variables and so on.</p><p>we added mixin and maps in the same partial file as its a simple example</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2cade0107a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to integrate your React app with Firebase]]></title>
            <link>https://medium.com/@samirasaad577/how-to-integrate-your-react-app-with-firebase-4bb9d200d69?source=rss-4531a331b66d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4bb9d200d69</guid>
            <dc:creator><![CDATA[samira saad]]></dc:creator>
            <pubDate>Wed, 04 Nov 2020 18:43:34 GMT</pubDate>
            <atom:updated>2020-11-13T13:39:26.719Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_en9mufnH_YWbuMj5bOVtA.jpeg" /></figure><p><strong>In this tutorial, we will learn how to get started by integrating the Firebase and Firestore cloud database with a React application.</strong></p><h3>Requirements</h3><p>to start this tutorial make sure you have:</p><ul><li>a Firebase account</li><li>NodeJS v8.x.x or higher installed along with npm/yarn</li><li>create-react-app global module to create a React project</li></ul><h3>Table of contents:</h3><ul><li>Getting started: New Firebase Project</li><li>Creating a Firestore Database</li><li>create React app</li><li>Connecting Firebase with React App</li></ul><h3>Getting Started: New Firebase Project</h3><p>To get started you need a Firebase account. To sign-up or log-in for an existing one, visit <a href="https://console.firebase.google.com/">console.firebase.com</a>. Once you are logged in, you will be welcomed by a screen like below,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tx4q9n7UbHzhU8HIihf8GQ.jpeg" /></figure><p>by clicking on <strong>Add Project</strong> button you will be redirected to new screen asking for naming your project</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*75LlbrfjY1su_PxOcH7g9w.jpeg" /></figure><p>give your project a name then click continue.</p><p>your next screen should be like below , click continue to go to the next screen</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gAac7zltEtAkClm9TsMAg.jpeg" /></figure><p>in the next screen, choose default account then press<strong> create project</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3WB-06X9uq_rQcHS5reeFQ.jpeg" /></figure><p><strong>wait a few seconds ,</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fDyMmAj596JiYuBrQsS2Bg.png" /></figure><p><strong>Done ^-^, </strong>just click continue</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CEVakvq6qEpUCR0i3PTEDg.jpeg" /></figure><p><strong>Now</strong> you have a new project on firebase,</p><p>your home screen should look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AfAitU4aUzn-C0qmXGyS5Q.jpeg" /></figure><h3>Creating a Firestore Database</h3><p>choose <strong>cloud firestore</strong> from the side menu</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S001I9-IY9-SY9GmiJ-kMw.jpeg" /></figure><p>you will be redirected to this screen, press create database</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o-uxiloQa9raT2_7P2Vlww.jpeg" /></figure><p>choose the second option start in test mode</p><p>then click next</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FZTZLY5IRp2XEtYC2QNHNg.png" /></figure><p>your next screen is a blank database waiting you to start a new collection</p><p>all our work will be in the first tab <strong>data</strong></p><p>just click start collection to add a new one</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PHzRpL3tfcGO-qDLZOkfTA.jpeg" /></figure><p>the next screen will display a popup asking to enter collection id</p><blockquote>collection id is just a collection name [ each collection has a unique name so its called id ]</blockquote><p>i will name it <strong>users</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rIh49plCfAUA8DgTxUUyYg.jpeg" /></figure><p>by clicking next will open new popup asking to fill document data</p><blockquote>each collection consists of a bunch of documents</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JCHCWAfWK2bpNoAibtgwMA.jpeg" /></figure><blockquote>document id is auto generated by clicking auto-id otherwise you should enter it with each document</blockquote><p>click add field to add additional data about the user, in our case is his age and enter each filed type</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QZR6RXV5_3zPNr_F1--kgQ.jpeg" /></figure><p>after clicking save your final database should look like below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mjnlHNETijQRMmR8tI2Edg.jpeg" /></figure><p>each time you want to add more users all you need is just clicking on add document and filling the user data in the same way we did before</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0CUIzpoNbABmwawkLeNcAA.png" /></figure><p>i added 2 more users</p><p>and here is my final database</p><p>contains one <strong>collection</strong> called users which has 3 docs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k5_WUu59vquS1_iCKl9heQ.png" /></figure><h3>Create React app</h3><p>to create new react app called <strong>react-firebase</strong></p><p>write the below command in your terminal</p><p>npx create-react-app react-firebase</p><p>move to the project directory using below command</p><p>cd react-firebase</p><p>then, npm start</p><p>your project will be launched on the browser with splash screen like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*whiRKMp8x1eapK3ImfpPWw.jpeg" /></figure><p>inside your src folder create new component and name it <strong>Home</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KLcyGF20y0ZWuyUzZQEDNw.png" /></figure><p>in App.js replace the returned jsx to be just &lt;Home /&gt; and do not forget to import it .</p><p>your <strong>App.js</strong> should look like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dgMck5JM3sEOL5eU-zHQug.jpeg" /></figure><p>and your browser now should display the <strong>Home</strong> component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FlmwmiSdJy2jV1mEZxxu8g.png" /></figure><p>Here we are , we have a firebase account contains our database and react app each of them work separately, w need them to work together so we can render users` data from <strong>database</strong> in our <strong>Home</strong> component</p><h3>Connecting Firebase with React App</h3><p><strong>Now,</strong> how we can make our react app recognize our database ?</p><p>we will introduce firebase to our react app by</p><p>providing <strong>firebase project config data </strong>to our <strong>react app</strong></p><blockquote>each firebase project has config object which is used to introduce firebase project to any other framework to be integrated with</blockquote><p>go back to your firebase project,</p><p>Click on the settings icon ⚙️ in the sidebar menu and go to <strong>Project settings</strong>. There you will see under <strong>Your apps</strong> section all the platforms available such as iOS, and web. Click on the <strong>Web option</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ngZ3htLI4O-K4qj4NTB9wQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*lxEC8Zf7jhXSx9JmXS_rjg.png" /></figure><p>just enter a nick name for your project to register a new app so you can get the <strong>config object </strong>then, click register app</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-8J6AR3vdoDZNlV0406hYQ.png" /></figure><p>you will get a screen like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pvANatY41j_K_uyjDhKP3g.png" /></figure><p>inside src directory make a new directory called <strong>firebase</strong> which holds file name it <strong>index.js</strong> will contain our <strong>api keys</strong> and <strong>initialization</strong> method</p><p><strong>Right now, </strong>to integrate Firebase with our React app, we only need to use<strong>initializeApp()</strong> method to pass all the keys required to configure firebase,</p><p>firebase/index.js :</p><blockquote>all xxxx s are the keys of the config object</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CCdk1l6Q-HFSSjoB5pNOkA.png" /></figure><p>In order to continue, React app needs FirebaseSDK installed as npm dependency. Open terminal window, make sure you are inside the project directory and run the following command.</p><p>npm install firebase --save</p><p><strong>Here we finished the integration between firebase and our react app</strong></p><p>once the installation is finished</p><p>go to the Home component and import <strong>db</strong> from the <strong>firebase</strong> directory</p><p>import db from ‘./../firebase’;</p><p>make simple useEffect hook to set users data into state once we got them</p><p>the home component should look like ,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9lsuwHkg85l783a_R6nQFQ.png" /></figure><p><strong>All we did here is :</strong></p><p>calling db instance using collection called users and get all its data then in success method then looping on each query (doc) and return doc data so, we get an array of objects each object produces user doc data in our case name and age</p><p>the last thing w need to do is to map on usersList array to render users` data inside the Home component</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xOkDomkZA17Rm1_t8dnXVQ.png" /></figure><p>on browser :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sv7YGGr9D_5Weug-0Ojllg.png" /></figure><p>what if i want to get specific user`s data not data of all users</p><p>just need to specify user document id</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v1eONbKKLGT3j7HLPPXU3w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w7Zz9-AtoQuf3GRzBDw3tQ.png" /></figure><h3>A very recommended tip :</h3><p>its highly recommended to put your config keys into env file cause its so sensitive data about your project and do not forget to include env file into gitignor as not tracked file</p><h3><strong>Very useful articles</strong></h3><ul><li><a href="https://www.robinwieruch.de/complete-firebase-authentication-react-tutorial">A Firebase in React Tutorial for Beginners [2019] - RWieruch</a></li><li><a href="https://medium.com/crowdbotics/how-to-integrate-firebase-firestore-react-and-crowdbotics-platform-59c35094c72e">How to integrate Firebase, Firestore, React and Crowdbotics Platform</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4bb9d200d69" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[setState in React js]]></title>
            <link>https://medium.com/@samirasaad577/setstate-in-react-js-5795f3f57020?source=rss-4531a331b66d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5795f3f57020</guid>
            <category><![CDATA[setstate]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[samira saad]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 14:35:05 GMT</pubDate>
            <atom:updated>2020-07-02T14:35:05.131Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Part 2: </strong>Three rules you should know when you use setState() in React js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*5UuoSa0-MrlQie14H2oBRA.jpeg" /></figure><blockquote><strong>here we go with part 2 of this series</strong></blockquote><blockquote>we learned in the past article how to update state, find part 1 <a href="https://medium.com/@samirasaad577/part-1-how-to-update-state-in-react-js-30be2212d034">here</a></blockquote><p>In this article we will talk about 3 rules you should be aware of when you use setState() method in react js.</p><p>lets assume that we have a class component like below with local state which has a property called inputValue has on-change event to store input value once it changed</p><p>have you ever tried to change component state like that ?!</p><pre>class InputField extends React.Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>    inputValue:&#39;&#39;<br>     }<br>  }<br>  <br>  handleChange = (e) =&gt;{<br>  this.state.inputValue = e.target.value;<br>  }</pre><pre>  render() {<br>  const {inputValue} = this.state<br>    return (<br>      &lt;form&gt;<br>      &lt;input value={inputValue} onChange={this.handleChange}/&gt;             <br>      &lt;/form&gt;<br>    )<br>  }<br>}</pre><p>tried it but didn`t work ?? nothing is appended to the input ??</p><h3><strong>Here is Why ?</strong></h3><p><strong>react js</strong> is working with an approach of re-rendering</p><p>class component re-renders in two cases if there is update in component local state or props</p><p><em>we`ve already updated local state object but why the component deosn`t get re-rendered</em> ??</p><p><strong>because of that</strong> : react js has only <strong>one way</strong> for setting / updating local state which is using built-in setState() method then, the component can re-render with the updated data</p><p>here is how to update local state using setState()</p><pre>class InputField extends React.Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>    inputValue:&#39;&#39;<br>     }<br>  }<br>  <br>  handleChange = (e) =&gt;{<br>  this.setState((prevState)=&gt;({inputValue:e.target.value}))</pre><pre>  }</pre><pre>render() {<br>  const {inputValue} = this.state<br>    return (<br>      &lt;form&gt;<br>        &lt;input value={inputValue} onChange={this.handleChange}/&gt;<br>      &lt;/form&gt;<br>    )<br>  }<br>}</pre><pre>export default InputField;</pre><p>input value changed ? <strong><em>Done</em></strong></p><h3><strong>Rule #1: </strong>Do Not Modify State Directly instead, use setState() method</h3><pre><br>//wrong<br>this.state.inputValue = e.target.value;<br></pre><pre>//correct<br>this.setState({inputValue:e.target.value})</pre><h3><strong>Rule #2 : Pass A Callback Function When Your Next Step Depends On Your Previous State</strong></h3><p>Assume that : we want to update state property on-change event and pass this new state property to AJAX request to fetch some data,</p><pre>class InputField extends React.Component {<br>  constructor(props) {<br>    super(props)<br>    this.state = {<br>    inputValue:&#39;&#39;,<br>    hasRequiredErr:false<br>     }<br>  }<br>  <br>  handleChange = (e) =&gt;{<br>    this.setState({inputValue:e.target.value})<br>}</pre><pre>handleSubmit = ()=&gt;{<br>e.preventDefault();<br>const {inputValue}=this.state;<br>this.setState({<br>hasRequired: inputValue === &#39;&#39;,<br>()=&gt;{<br>const {hasRequired}=this.state;<br>const {MyRequest} = this.props;<br>      if(!hasRequired){<br>      //fetch data<br>      MyRequest({query : inputValue})<br>   }}<br>})</pre><pre>render() {<br>  const {inputValue} = this.state<br>    return (<br>      &lt;form onSubmit={this.handleSubmit}&gt;<br>      &lt;input value={inputValue} onChange={this.handleChange}/&gt;<br>      &lt;/form&gt;<br>    )<br>  }<br>}<br>export default InputField;</pre><p>-use callback function to use updated state in your next operation</p><h3><strong>** Tip</strong> : another approach rather than using callback:</h3><pre>componentDidUpdate = () =&gt;{<br>const {MyRequest} = this.props;</pre><pre>if( prevState.inputValue !== this.state.inputValue){<br>     //fetch<br>      MyRequest({query : inputValue})<br>  }</pre><pre>}</pre><h3>Rule #3: Be Careful setState() Is An Asynchronous Operation</h3><ul><li><strong>this.props </strong>and <strong>this.state</strong> may be updated asynchronously, you should not rely on their values to know the next state.</li><li>avoid multiple setState in the same scope , to better performance merge all updates in one setState calling</li></ul><pre><em>//recommended to be avoided</em><br>if(this.state.inputValue === &#39;&#39;){<br>   this.setSatte({value:&#39;hello&#39;})<br>   this.setSatte(ValueLength:5})<br>   this.setSatte({foo:{name:&#39;john&#39;,age:25}<br>  })<br>}</pre><pre><em>//better solution</em><br>if(this.state.inputValue === &#39;&#39;){<br>   this.setState(<br>     {value:&#39;hello&#39;,<br>      ValueLength:5,<br>      foo:{name:&#39;john&#39;,age:25<br>    }<br>  })<br>}</pre><p><strong>useful reference</strong></p><p><a href="https://medium.com/@wereHamster/beware-react-setstate-is-asynchronous-ce87ef1a9cf3">https://medium.com/@wereHamster/beware-react-setstate-is-asynchronous-ce87ef1a9cf3</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5795f3f57020" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Part 1 : How to update state in React.js]]></title>
            <link>https://medium.com/@samirasaad577/part-1-how-to-update-state-in-react-js-30be2212d034?source=rss-4531a331b66d------2</link>
            <guid isPermaLink="false">https://medium.com/p/30be2212d034</guid>
            <category><![CDATA[setstate]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[samira saad]]></dc:creator>
            <pubDate>Thu, 02 Jul 2020 14:15:04 GMT</pubDate>
            <atom:updated>2020-07-02T14:15:04.391Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Part 1 : </strong>How to update state in React.js</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*HZDPos8fa5v4rG_u9QXneQ.jpeg" /></figure><p>React separates passing data &amp; manipulating data using props and state</p><p>Props are only being used for passing data. They are <strong>read-only</strong> which means that components receiving data by props are not able to change it. However, in some cases, a component may need to manipulate data and that’s not possible with props.</p><p>So React provides another feature for data manipulation which is known as <strong>State</strong>. In this article, you will learn what React’s <strong>“State”</strong> is and how to use it</p><p><strong>lets start our journey,</strong></p><h3>what is state in react ?</h3><ul><li>each component (class component )in react has its own state</li><li>state is initialized inside its component’s <strong>constructor</strong> method.</li><li>state is safe isolated place where component can stores its data</li><li>one component does not know about others`s state</li><li>state is a javascript object could hold one or multiple values</li></ul><h3><strong>How to update state ?</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c856d3f67861c6197b65f4533ffebf5c/href">https://medium.com/media/c856d3f67861c6197b65f4533ffebf5c/href</a></iframe><p>in vanilla javascript we can update objects property like that</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41796b0865676e9f0e2a2c45a1a8bba4/href">https://medium.com/media/41796b0865676e9f0e2a2c45a1a8bba4/href</a></iframe><p>in react component we can not update state object directly</p><h3><strong>why ??</strong></h3><p>state object in react has different way to be updated</p><p>react provides a built in method called setState() to update the state object inside a component</p><p>as react work with an approach of re-rendering to display components with newely updated data</p><p><strong>how to use setState() to update component state ?</strong></p><p>assume that we have a form like below to enter data to schedule an interview ,this form holds inputs for interviewer name, interviewee info which is a nested object contains interviewee name , address which is also nested object contains city and street also nested object has name and street number</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/502/1*Lp1ewNXpa258ZqjGHeMS4w.png" /></figure><p>we need to update each change happen inside any input as soon as it detects and store it in its state property</p><p>our state properties :</p><p><strong>first</strong> level property : interviewerName,</p><p><strong>second</strong> level property : intervieweeName,</p><p><strong>third</strong> level property : city,</p><p><strong>fourth</strong> level property: streetName, streetNumber</p><h3>1- Updating first level property</h3><p>it matches interviewerName</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/af0360c0dd44afac67bae64fb6699f90/href">https://medium.com/media/af0360c0dd44afac67bae64fb6699f90/href</a></iframe><p>we have input for interviewer name once we entered a value in it the on-change handler checks for input name in simple switch case if it is `<strong>interviewer-name</strong>` set its value in state directly, to the interviewerName property inside state</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7aaa19dd1d6b84f0d0ee0bd70c40a4c/href">https://medium.com/media/c7aaa19dd1d6b84f0d0ee0bd70c40a4c/href</a></iframe><p>click submit and you will see your state updates printed to the console or,</p><p>you can use react chrome extension to detect state once it changed</p><p>just open your developer tool -&gt; components tab -&gt; search for your component</p><p>and you`ll get your component state like so,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*zTpm0laBAIhzbBQB-dCFEQ.png" /></figure><h3>2- Updating 2nd level property</h3><p>it matches interviewee-name property</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3873237dec1a784c8266368dd02beadc/href">https://medium.com/media/3873237dec1a784c8266368dd02beadc/href</a></iframe><p>** Just add the interviewee name input to jsx part and its case to change handler method</p><p>the same scenario will be done on the new input , checking for input name then set its value to the matched state property So,</p><p><strong>what is the difference ?</strong></p><p>the difference is the way we updating non-1st level property inside state object, we have additional step,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fc36ec8b7af86e887eec2791a51c45d1/href">https://medium.com/media/fc36ec8b7af86e887eec2791a51c45d1/href</a></iframe><p><strong>Explanation</strong>:</p><p>1- we entered something into the input</p><p>2- on-change event fires, checks for input name and found it equals to `interviewee-name`</p><p>in switch case of `interviewee-name`</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a5bae7218e725b4f51c48ed4f957d1d/href">https://medium.com/media/9a5bae7218e725b4f51c48ed4f957d1d/href</a></iframe><p>3- we go inside the state object and call a property called intervieweeInfo</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f68651ebdb9c9ee1eeecade7720c2e84/href">https://medium.com/media/f68651ebdb9c9ee1eeecade7720c2e84/href</a></iframe><p>4- spread intervieweeInfo properties</p><blockquote>We use spread operator to make sure the other properties inside this object don`t change</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/861ccef167ca1ff6b8566b0815b03fc9/href">https://medium.com/media/861ccef167ca1ff6b8566b0815b03fc9/href</a></iframe><h3><strong>Tip :</strong></h3><p>the first interviewInfo is the object key we can not spread it</p><p>the second intervieweeInfo we need to spread it as it refers to the object values we want to keep them,</p><h3><strong><em>In other words</em></strong></h3><blockquote><em>…intervieweeInfo</em><strong><em> </em>means that keep this object properties values the same as you found them</strong></blockquote><p>5- keep all values of intervieweeInfo object the same as you found it except one property called intervieweeName update it with the input value then break.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73dffd81ca9cb7679eee3ab6ba465653/href">https://medium.com/media/73dffd81ca9cb7679eee3ab6ba465653/href</a></iframe><h3>3- Updating 3rd level property</h3><p>it matches city property</p><p>** Just add the rest of inputs to your jsx to continue our form</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/49e2fe898f1c006f48ec20b2b431e0af/href">https://medium.com/media/49e2fe898f1c006f48ec20b2b431e0af/href</a></iframe><p>the same will be done to update city,but with single additional step which is going into intervieweeAddress object and inside it spread it to save the rest values of this object and change only one property which is called city with the input value</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99d15492789175381da1c16f919fddab/href">https://medium.com/media/99d15492789175381da1c16f919fddab/href</a></iframe><h3>4- Updating 4th level property</h3><p>it matches street name and number properties</p><p><strong><em>the same will be done with street name and street number with another additional step which is ??</em></strong></p><p>right, going into the street object and then spread it to save the number while we update name and save the name while we update number</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4ac83882dc4ab45af4b30a6a9898dce0/href">https://medium.com/media/4ac83882dc4ab45af4b30a6a9898dce0/href</a></iframe><p>finally, you can see the final result here :</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fv1gx8&amp;display_name=CodeSandbox&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fv1gx8&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Fv1gx8%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/80d2fabe9c2c1cbefd57595aca7cf634/href">https://medium.com/media/80d2fabe9c2c1cbefd57595aca7cf634/href</a></iframe><p>** <strong>another approach of setting state is to pass function returns an object instead of direct object like so :</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07c88396190b683cefd5d4ec761308e4/href">https://medium.com/media/07c88396190b683cefd5d4ec761308e4/href</a></iframe><p><strong><em>summary:</em></strong></p><p>As we mentioned state in react is a javascript object holds properties</p><p>if it was a first level property update it directly as a pair of key:value</p><p>otherwise, use spread operator to update it</p><p>in both cases use setState() react method to update it</p><blockquote>there will be <strong>part 2</strong> of our series to talk about rules should be considered when u use setState().</blockquote><p><strong>helpful references :</strong></p><ul><li><a href="https://daveceddia.com/visual-guide-to-state-in-react/">A Visual Guide to State in React</a></li><li><a href="https://medium.com/javascript-in-plain-english/why-you-should-stop-using-objects-with-setstate-ee7a6c7bcba3">Why You Should Stop Using Objects With SetState()</a></li></ul><p><a href="https://medium.com/better-programming/when-to-use-callback-function-of-setstate-in-react-37fff67e5a6c">https://medium.com/better-programming/when-to-use-callback-function-of-setstate-in-react-37fff67e5a6c</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30be2212d034" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ES6 Features:Function Default Parameters]]></title>
            <link>https://medium.com/@samirasaad577/es6-features-function-default-parameters-8608dd11f8c6?source=rss-4531a331b66d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8608dd11f8c6</guid>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[samira saad]]></dc:creator>
            <pubDate>Sun, 17 Nov 2019 12:39:17 GMT</pubDate>
            <atom:updated>2019-11-20T10:11:26.467Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="laptop display some code" src="https://cdn-images-1.medium.com/max/1024/1*WLNNomhONO7lQ9itTV2p6Q.jpeg" /></figure><p><em>in this article we will learn how to deal with Javascript new feature :</em></p><p><em>Function Default Parameters in ES6</em>.</p><h3>First Of All,<br><strong>Argument vs. Parameters in javascript</strong></h3><p>we always use the term of argument and parameter,<br>the question is, when we call it a parameter and when an argument ?!</p><p><strong>1- </strong>it is called parameter when it is declared in the function declaration<br>like that,</p><p><strong>Declare:</strong></p><pre>function addTwoNumbers(x,y){<br>return x+y;}</pre><p><strong>Explanation: </strong>addTwoNumbers function declarrion has two parameters <br> x called parameter 1 , y called parameter 2</p><p><strong>2- </strong>it is called argument when it is passed in the function calling<br>like that,</p><p><strong>Call:</strong></p><pre>addTwoNumbers(2,3); //5  </pre><p><strong>Explanation: </strong>addTwoNumbers function calling has two arguments<br> x called argument 1 , y called argument 2</p><h3><strong>what if we passed one argument in case the function need to two ??</strong></h3><p>javascript till ES5 has a technique to deal with that</p><blockquote><em>if i am a function in ES5 and need to 2 arguments and you passed to me only one argument,<br>i will ignore the other one and consider it`s value as undefined <br>and will not even bring you an error</em></blockquote><p><strong>lets</strong> apply this technique here with our ES5 function</p><pre>addTwoNumbers(2); //2+undefined = NaN<br></pre><p><strong>Explanation: </strong>addTwoNumbers function calling has one argument<br>in case it needs to two arguments so one of them will be considered as undefined according to ES5 behavior</p><p><strong>but now , why Nan ??</strong></p><p><strong><em>Tip: </em></strong>NaN is refers to<strong> N</strong>ot<strong> A N</strong>umber</p><p><strong>there is something we are missing here ,</strong><br>`we can not add a number to another value unless it is number too` this is why we get Nan as a result</p><p>thanks to <strong><em>ES6 feature called function default parameters</em></strong> we can avoid this issue</p><blockquote>Default function parameters allow named parameters to be<br> initialized with default values if no value or undefined is passed</blockquote><p>lets see the difference using ES6 new feature</p><p><strong>Declare:</strong></p><pre><br>function addTwoNumbers(x =0 , y=0 ){<br>return x+y;<br>}</pre><p><strong>Call:</strong></p><pre><br>addTwoNumbers(2);//2!!!<br></pre><p><strong>how??</strong></p><p>when we call addTwoNumbers function it realize that it has default parameters<br>if it does not get another value for any of it`s parameters in calling time it will use the default one</p><pre>addTwoNumbers();//0+0=0</pre><pre>addTwoNumbers(2,3);//2+3=5</pre><pre>addTwoNumbers(2);//2+0=2</pre><h3><strong>conclusion</strong></h3><p>function default parameter is new feature in Es6 it allows us to make initial values for function parameters to avoid undefined value for unpassed arguments,</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8608dd11f8c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>