<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shaban-Eissa</title>
    <description>The latest articles on DEV Community by Shaban-Eissa (@shabaneissa).</description>
    <link>https://dev.to/shabaneissa</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F435069%2F94e62161-0e73-4b24-8e88-8fd87d90d701.jpg</url>
      <title>DEV Community: Shaban-Eissa</title>
      <link>https://dev.to/shabaneissa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shabaneissa"/>
    <language>en</language>
    <item>
      <title>Don't Update State Directly In React</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Wed, 05 Oct 2022 11:15:54 +0000</pubDate>
      <link>https://dev.to/shabaneissa/dont-update-state-directly-in-react-43o6</link>
      <guid>https://dev.to/shabaneissa/dont-update-state-directly-in-react-43o6</guid>
      <description>&lt;p&gt;In this post, we will discuss how to update state in correct way &lt;/p&gt;

&lt;p&gt;first, App.js that contain list of todos and pass them as props to Todos component, after this we receive props in Todos component and passing them as props to Todo component to render every todo in list of todos, in Todos.js there is handleToggleComplete to make checkbox marked or not and contain handleSelect to detect which todo is selected &lt;/p&gt;

&lt;p&gt;App.js code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Todos from "./Todos";

export default function App() {

  const initialTodos = [
    {
      id : 1 ,
      complete : false ,
      name : 'first todo'
    } , 
    {
      id : 2 ,
      complete : true ,
      name : 'second todo'
    } ,
    {
      id : 3 ,
      complete : false ,
      name : 'third todo'
    }
  ]
  return (
    &amp;lt;div className="App"&amp;gt;
     &amp;lt;Todos initialTodos = {initialTodos} /&amp;gt;  
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Todos.js code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import Todo from "./Todo";

const Todos = ({ initialTodos }) =&amp;gt; {
  const [todos, setTodos] = useState(initialTodos);
  const [selectedTodo, setSelectedTodo] = useState();

  const handleToggleComplete = (todoId) =&amp;gt; {
    setTodos((currTodos) =&amp;gt; {
      const todo = currTodos.find((todo) =&amp;gt; todo.id === todoId);
      todo.complete = !todo.complete;
      return currTodos;
    });
  };

  const handleSelect = (todoId) =&amp;gt; {
    setSelectedTodo(todos.find((todo) =&amp;gt; todo.id === todoId));
  };

  return (
    &amp;lt;&amp;gt;
      {todos.map((todo) =&amp;gt; (
        &amp;lt;Todo
          key={todo.id}
          todo={todo}
          handleToggleComplete={handleToggleComplete}
          handleSelect={handleSelect}
        /&amp;gt;
      ))}
      &amp;lt;h3&amp;gt;Selected Todo&amp;lt;/h3&amp;gt;

      {selectedTodo &amp;amp;&amp;amp; (
        &amp;lt;Todo
          todo={selectedTodo}
          handleToggleComplete={handleToggleComplete}
          handleSelect={handleSelect}
        /&amp;gt;
      )}
    &amp;lt;/&amp;gt;
  );
};

export default Todos;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Todo.js code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

const Todo = ({ todo, handleToggleComplete, handleSelect }) =&amp;gt; {
  const toggleComplete = () =&amp;gt; {
    handleToggleComplete(todo.id);
  };

  const onSelect = () =&amp;gt; {
    handleSelect(todo.id);
  };
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;input
        type="checkbox"
        checked={todo.complete}
        onChange={toggleComplete}
      /&amp;gt;
      {todo.name}
      &amp;lt;button onClick={onSelect}&amp;gt;Select&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Todo;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now let's see the result of this code &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frs9dptliuaujbystp7hb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frs9dptliuaujbystp7hb.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as we see we can't update any checkbox, this because we update state directly, in setTodos we update currTodos directly and return currTodos&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const handleToggleComplete = (todoId) =&amp;gt; {
    setTodos((currTodos) =&amp;gt; {
      const todo = currTodos.find((todo) =&amp;gt; todo.id === todoId);
      todo.complete = !todo.complete;
      return currTodos;
    });
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to solve this problem we can update state by take copy of currTodos and update through copied state&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleToggleComplete = (todoId) =&amp;gt; {
    setTodos((currTodos) =&amp;gt; {
      const copy = [...currTodos]
      return copy.map(todo =&amp;gt; {
        if(todo.id === todoId) {
          return {...todo  , complete : !todo.complete}
        }
        return todo 
      })
    });
  };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7begn3gtfhm0hfjsm4on.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7begn3gtfhm0hfjsm4on.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;to make more clarification about how to update state in code above,&lt;br&gt;
1- copy is array we make update through it &lt;br&gt;
2- if there is update return {...todo  , complete : !todo.complete} this will be updated in copy array &lt;br&gt;
3- if there is no update return todo &lt;br&gt;
4- all this array (copy array) will be returned as new state as we say setTodos(copy)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; return copy.map(todo =&amp;gt; {
        if(todo.id === todoId) {
          return {...todo  , complete : !todo.complete}
        }
        return todo 
      })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's see the result now : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvf9t38af8vplijcwc9od.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvf9t38af8vplijcwc9od.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now we can update state of each checkbox successfully, but let's see what about the selectedTodo :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxtfg98qxzj31tfgii0r.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpxtfg98qxzj31tfgii0r.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;as we see, we selected third todo, but when mark or unmark checkbox from third todo it didn't update on selectedTodo that is becuase we stored the derived state of selectedTodo&lt;/p&gt;

&lt;p&gt;in this code below the wrong is we store derived state, so keep in mind don't store derived state &lt;br&gt;
&lt;code&gt;setSelectedTodo(todos.find((todo) =&amp;gt; todo.id === todoId))&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;the correct way is using this line of code below so every re-render we calculate updated selectedTodo and we can using useMemo for this&lt;br&gt;
&lt;code&gt;const selectedTodo = todos.find(todo =&amp;gt; todo.id === selectedTodoId)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and that is the code below in Todos.js with all changes&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";
import Todo from "./Todo";

const Todos = ({ initialTodos }) =&amp;gt; {
  const [todos, setTodos] = useState(initialTodos);
  const [selectedTodoId, setSelectedTodoId] = useState();

  const selectedTodo = todos.find(todo =&amp;gt; todo.id === selectedTodoId)

  const handleToggleComplete = (todoId) =&amp;gt; {
    setTodos((currTodos) =&amp;gt; {
      const copy = [...currTodos]
      return copy.map(todo =&amp;gt; {
        if(todo.id === todoId) {
          return {...todo  , complete : !todo.complete}
        }
        return todo 
      })
    });
  };

  return (
    &amp;lt;&amp;gt;
      {todos.map((todo) =&amp;gt; (
        &amp;lt;Todo
          key={todo.id}
          todo={todo}
          handleToggleComplete={handleToggleComplete}
          handleSelect={setSelectedTodoId}
        /&amp;gt;
      ))}
      &amp;lt;h3&amp;gt;Selected Todo&amp;lt;/h3&amp;gt;

      {selectedTodo &amp;amp;&amp;amp; (
        &amp;lt;Todo
          todo={selectedTodo}
          handleToggleComplete={handleToggleComplete}
          handleSelect={setSelectedTodoId}
        /&amp;gt;
      )}
    &amp;lt;/&amp;gt;
  );
};

export default Todos;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;let's see the result now of this code : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1yhybv3h9lchsnpho3t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm1yhybv3h9lchsnpho3t.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is all about how to update state in correct way, keep in mind always not update state directly, take copy of state and update through the copied state &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgu3z6njekusfuet3ajf7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgu3z6njekusfuet3ajf7.gif" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Thanks for your time to read this post &lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>React UseEffect Best Practice</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Sun, 02 Oct 2022 14:39:06 +0000</pubDate>
      <link>https://dev.to/shabaneissa/react-useeffect-best-practice-248k</link>
      <guid>https://dev.to/shabaneissa/react-useeffect-best-practice-248k</guid>
      <description>&lt;p&gt;In this post we will see different cases of useEffect &lt;/p&gt;

&lt;p&gt;first, let's see useEffect without any dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
import "./styles.css";

export default function App() {
  const [name, setName] = useState("");
  const [dark, setDark] = useState(false);

  const age = 23
  const user = { name, age };

  const buttonStyle = {
    backgroundColor: dark &amp;amp;&amp;amp; "#000",
    color: dark &amp;amp;&amp;amp; "#FFF"
  };

  const handleName = (e) =&amp;gt; {
    setName(e.target.value);
  };

  const handleClick = () =&amp;gt; {
    setDark((currDark) =&amp;gt; !currDark);
  };

  useEffect(() =&amp;gt; {
    console.log(user);
  });

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;input value={name} onChange={handleName} /&amp;gt;
      &amp;lt;button style={buttonStyle} onClick={handleClick}&amp;gt;
        Toggle Theme
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this code, any change in any state will cause re-render and then useEffect run again and again, so let's see the result in console when code run &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo1x7sogekwraohv72nw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo1x7sogekwraohv72nw.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;second, let's see useEffect with dependency array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useState } from "react";
import "./styles.css";

export default function App() {
  const [name, setName] = useState("");
  const [dark, setDark] = useState(false);

  const age = 23
  const user = { name, age };

  const buttonStyle = {
    backgroundColor: dark &amp;amp;&amp;amp; "#000",
    color: dark &amp;amp;&amp;amp; "#FFF"
  };

  const handleName = (e) =&amp;gt; {
    setName(e.target.value);
  };

  const handleClick = () =&amp;gt; {
    setDark((currDark) =&amp;gt; !currDark);
  };

  useEffect(() =&amp;gt; {
    console.log(user);
  } , [user]);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;input value={name} onChange={handleName} /&amp;gt;
      &amp;lt;button style={buttonStyle} onClick={handleClick}&amp;gt;
        Toggle Theme
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this code useEffect will run when any change occur in user, now we can see that user is object and every re-render user object will have different reference, i mean we don't have the same reference for user object everytime so when we click on button we can see that the effect run !!!!!!!!! &lt;br&gt;
we make effect depend on user object only, but why effect happen when we click on button, the normal is effect happen only when the user object value change, and so the answer for why when click on button effect runs is that because different reference of object. &lt;/p&gt;

&lt;p&gt;let's see the result of this code : &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo1x7sogekwraohv72nw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyo1x7sogekwraohv72nw.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and to solve this problem we can replace dependency array of useEffect by &lt;code&gt;[name , age]&lt;/code&gt;, then now we avoided different reference of user object&lt;/p&gt;

&lt;p&gt;so let's see the result now &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2ev1nkjh6z3qfcwgzod.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu2ev1nkjh6z3qfcwgzod.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;that is what we need now &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F223is1wsbp5aayzyefwx.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F223is1wsbp5aayzyefwx.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;an alternative solution to that with same result, we can keep user object in dependency and using useMemo() on user object like this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useMemo, useState } from "react";
import "./styles.css";

export default function App() {
  const age = 23;
  const [name, setName] = useState("");
  const [dark, setDark] = useState(false);

  const user = useMemo(() =&amp;gt; {
    return { name, age };
  }, [name, age]);

  const buttonStyle = {
    backgroundColor: dark &amp;amp;&amp;amp; "#000",
    color: dark &amp;amp;&amp;amp; "#FFF"
  };

  const handleName = (e) =&amp;gt; {
    setName(e.target.value);
  };

  const handleClick = () =&amp;gt; {
    setDark((currDark) =&amp;gt; !currDark);
  };

  useEffect(() =&amp;gt; {
    console.log(user);
  }, [user]);

  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;input value={name} onChange={handleName} /&amp;gt;
      &amp;lt;button style={buttonStyle} onClick={handleClick}&amp;gt;
        Toggle Theme
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;with useMemo() the user object reference will change only if any change occurred in name or age, then useEffect will run only if any change occurred in name or age.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncvwiogan71hy3mfg1wq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fncvwiogan71hy3mfg1wq.gif" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>Console current value of state in react js</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Sun, 02 Oct 2022 12:31:51 +0000</pubDate>
      <link>https://dev.to/shabaneissa/console-current-value-of-state-in-react-js-28ho</link>
      <guid>https://dev.to/shabaneissa/console-current-value-of-state-in-react-js-28ho</guid>
      <description>&lt;p&gt;we have a button and counter, we need once we clicked on button to console the current value of counter not the previous value &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GCT-ux_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbn5ta9s738ppz3mb6wq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GCT-ux_A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbn5ta9s738ppz3mb6wq.png" alt="Image description" width="880" height="903"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This code will display the previous value of counter because the state is asynchronous so console.log(counter) will run first before state changed, and that is why we see the previous value of counter when write console.log(counter) in increment function &lt;/p&gt;

&lt;p&gt;That is result of above code &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EU166wAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1jm98hq91yjyahyfsxr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EU166wAo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y1jm98hq91yjyahyfsxr.gif" alt="Image description" width="638" height="511"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;we can solve this by using useEffect and write console.log() in useEffect like this code : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BZmG_yEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgmyfsjas9s2jsxw2icu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BZmG_yEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgmyfsjas9s2jsxw2icu.png" alt="Image description" width="880" height="974"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and now if we clicked on button mean that we changed the state of counter and then useEffect will run becuase it depend on counter state, so we can see the current value of counter in the console &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UnSMjNkD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks19ulqcpm04s0jyp33z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UnSMjNkD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ks19ulqcpm04s0jyp33z.gif" alt="Image description" width="624" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>Updating state in react based on the previous state</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Sat, 01 Oct 2022 16:53:50 +0000</pubDate>
      <link>https://dev.to/shabaneissa/updating-state-in-react-based-on-the-previous-state-1373</link>
      <guid>https://dev.to/shabaneissa/updating-state-in-react-based-on-the-previous-state-1373</guid>
      <description>&lt;p&gt;Suppose the age is 42, let's see the difference between first and second function&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fvtqM_Uo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fh1kw3x1rf6arr025zbj.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fvtqM_Uo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fh1kw3x1rf6arr025zbj.jpg" alt="Image description" width="880" height="477"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in first function, after one click, the age will only be 43 rather than 45! This is because calling the set function does not update the age state variable in the already running code. So each setAge(age + 1) call becomes setAge(43).&lt;/p&gt;

&lt;p&gt;in second function, you may pass an updater function to setAge and Here, age =&amp;gt; age + 1 is your updater function. It takes the pending state and calculates the next state from it.&lt;br&gt;
React puts your updater functions in a queue. Then, during the next render, it will call them in the same order:&lt;br&gt;
age =&amp;gt; age + 1 will receive 42 as the pending state and return 43 as the next state.&lt;br&gt;
age =&amp;gt; age + 1 will receive 43 as the pending state and return 44 as the next state.&lt;br&gt;
age =&amp;gt; age + 1 will receive 44 as the pending state and return 45 as the next state. There are no other queued updates, so React will store 45 as the current state in the end.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vW0peB1o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgticpsi1geksviacwgw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vW0peB1o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgticpsi1geksviacwgw.gif" alt="Image description" width="200" height="143"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>useState Best Practice</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Fri, 30 Sep 2022 13:04:40 +0000</pubDate>
      <link>https://dev.to/shabaneissa/usestate-best-practice-ca3</link>
      <guid>https://dev.to/shabaneissa/usestate-best-practice-ca3</guid>
      <description>&lt;p&gt;let's see in this post how we can apply best practice for &lt;code&gt;useState()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;suppose we have many inputs and we need to make state for each input, normal way to make this will be like : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tHQ3UcrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pmb1g0n1u710f17wgcd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tHQ3UcrT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5pmb1g0n1u710f17wgcd.png" alt="Image description" width="880" height="1868"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is avoidable because it will consume more time and the code will be un-readable; so the another way and best practice to make  is to create state as object and every key is the name of each input such as &lt;br&gt;
&lt;code&gt;const [inputs,setInput] = useState({&lt;br&gt;
name : "" ,&lt;br&gt;
email : "" ,&lt;br&gt;
password : ""})&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input name="name" /&amp;gt;&lt;br&gt;
&amp;lt;input name="email" /&amp;gt;&lt;br&gt;
&amp;lt;input name="password" /&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and there is full code with only one function called handleChange that can handle all inputs &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KltkHcej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdalwy5d5xad9ec1pig2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KltkHcej--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdalwy5d5xad9ec1pig2.png" alt="Image description" width="880" height="1705"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>React Re-render Best Practice</title>
      <dc:creator>Shaban-Eissa</dc:creator>
      <pubDate>Thu, 29 Sep 2022 15:11:17 +0000</pubDate>
      <link>https://dev.to/shabaneissa/react-js-re-render-best-practice-557d</link>
      <guid>https://dev.to/shabaneissa/react-js-re-render-best-practice-557d</guid>
      <description>&lt;p&gt;In this post i will discuss how if we split the components can affect the performance of our react app.&lt;/p&gt;

&lt;p&gt;first, the code will be two components :&lt;/p&gt;

&lt;p&gt;This is first component&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XE5Pa3tY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j76s9zifhdvfh2u0lo0u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XE5Pa3tY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j76s9zifhdvfh2u0lo0u.png" alt="Image description" width="880" height="950"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is second component that we imported it in the component above&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NLVpTf1b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar9e523677eyiog981s8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NLVpTf1b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar9e523677eyiog981s8.png" alt="Image description" width="790" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's check console :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--okDhEMYY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geswswr13taakot501xv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--okDhEMYY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/geswswr13taakot501xv.gif" alt="Image description" width="508" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now we see every time we click on the button, the state changed and then re-render the component then &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; will be rendered every time we click on button !!!!!!!!, we don't need that to happen because the &lt;code&gt;&amp;lt;Child /&amp;gt;&lt;/code&gt; don't have any code that is relevant to main component, in other words don't have any relation to the color state, so we don't need to re-render it again because we clicked on the button &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1c2fCQ5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5wv7wb1rdt20hrpvey6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1c2fCQ5u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5wv7wb1rdt20hrpvey6.gif" alt="Image description" width="200" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution of This problem :
&lt;/h2&gt;

&lt;p&gt;first we need to make new file contain these two components like this below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7DU3mDlJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mcdfx6r8no1hhxwfma2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7DU3mDlJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mcdfx6r8no1hhxwfma2.png" alt="Image description" width="880" height="730"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and these two components now : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OcVNqEZp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16aaegmgkns2zv59vz5q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OcVNqEZp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16aaegmgkns2zv59vz5q.png" alt="Image description" width="880" height="841"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Xa_CSbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opzzbnjbt5u3no0w1zhh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Xa_CSbD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/opzzbnjbt5u3no0w1zhh.png" alt="Image description" width="756" height="598"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;now let's see the console : &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3iO2Tkqx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i72a3rgftej5ylg7agrt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3iO2Tkqx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i72a3rgftej5ylg7agrt.gif" alt="Image description" width="507" height="446"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;we can see that we can click the button and there is no more un-necessary render and that is the required.&lt;/p&gt;

&lt;p&gt;That is my first post in dev.to and in the future i want to share more and more about react in this website, so leave your feedback about the post to make the next posts more organized, more useful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6WkFoU_x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ous1fvhrmtj41z48pfmy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6WkFoU_x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ous1fvhrmtj41z48pfmy.gif" alt="Image description" width="356" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
