<?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: adelchms96</title>
    <description>The latest articles on DEV Community by adelchms96 (@adelchms96).</description>
    <link>https://dev.to/adelchms96</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%2F399040%2Fb9f89ec4-d2ae-4cf7-b3eb-dd732662ce0c.png</url>
      <title>DEV Community: adelchms96</title>
      <link>https://dev.to/adelchms96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/adelchms96"/>
    <language>en</language>
    <item>
      <title>react hook for waiting state update (useAsyncState)</title>
      <dc:creator>adelchms96</dc:creator>
      <pubDate>Sun, 13 Dec 2020 11:30:33 +0000</pubDate>
      <link>https://dev.to/adelchms96/react-hook-for-waiting-state-update-useasyncstate-147g</link>
      <guid>https://dev.to/adelchms96/react-hook-for-waiting-state-update-useasyncstate-147g</guid>
      <description>&lt;p&gt;hello guys, React is an awesome library especially after introducing hooks in function component which help us minimize the code writing already written in class components, but this didn't change how react work especially state update which happen asynchronous. &lt;br&gt;
So image we want to invoke a function on each state update, we need to invoke it from &lt;code&gt;useEffect&lt;/code&gt; like this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [state, setState] = useState(0);
useEffect(()=&amp;gt;{
console.log(state);
},[state])
  return (
    &amp;lt;button onClick={()=&amp;gt;setState(prev =&amp;gt; prev + 1)}&amp;gt;
      {state}
      increment
    &amp;lt;/button&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;but why we can't have to wait for set update to happen then invoke our function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const currentState = await setState(prev =&amp;gt; prev + 1);
 console.log(currentState);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this what &lt;code&gt;useAsyncState&lt;/code&gt; come for have a look at the 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 { useState, useRef, useCallback, useEffect } from "react";

function useAsyncState(initialState) {
  const [state, setState] = useState(initialState);
  const resolveState = useRef();
  const isMounted = useRef(false);

  useEffect(() =&amp;gt; {
    isMounted.current = true;

    return () =&amp;gt; {
      isMounted.current = false;
    };
  }, []);

  useEffect(() =&amp;gt; {
    if (resolveState.current) {
      resolveState.current(state);
    }
  }, [state]);

  const setAsyncState = useCallback(
    newState =&amp;gt;
      new Promise(resolve =&amp;gt; {
        if (isMounted.current) {
          resolveState.current = resolve;
          setState(newState);
        }
      }),
    []
  );

  return [state, setAsyncState];
}

export default useAsyncState;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;usage no need for the &lt;code&gt;useEffect&lt;/code&gt; anymore :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  const [state, setState] = useAsyncState(0);
  const click = async () =&amp;gt; {
    const currentState = await setState(prev =&amp;gt; prev + 1);
    console.log(currentState);
  };
  return (
    &amp;lt;button onClick={click}&amp;gt;
      {state}
      increment
    &amp;lt;/button&amp;gt;
  );
}

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

&lt;/div&gt;



</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>UseFetch hook for react js.</title>
      <dc:creator>adelchms96</dc:creator>
      <pubDate>Fri, 31 Jul 2020 17:25:56 +0000</pubDate>
      <link>https://dev.to/adelchms96/usefetch-hook-for-react-js-2f3i</link>
      <guid>https://dev.to/adelchms96/usefetch-hook-for-react-js-2f3i</guid>
      <description>&lt;p&gt;hello guys, will show you a custom hook for handling async call.&lt;br&gt;
&lt;code&gt;useFetch&lt;/code&gt; hook take a &lt;code&gt;callback&lt;/code&gt; as first argument and &lt;code&gt;callback&lt;/code&gt; params for rest arguments. &lt;br&gt;
here it's:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const useFetch = (cb, ...params) =&amp;gt; {
  const isMounted = useRef();
  const [response, setResponse] = useState();
  const [loading, setLoading] = useState(false);
  //const [error, setError] = useState();

  return {
    response,
    loading,
    isMounted,
    reset: () =&amp;gt; setResponse(),
    fetch: async (reload = false) =&amp;gt; {
      try {
        isMounted.current = true;
        if (!response || reload) setLoading(true);
        const data = await cb(...params);
        if (isMounted.cuurent) {
          if (data) setResponse(data)
        }
      } catch (error) {
        errorNotification(error); // do something with the error
        // or u can add setError(error)
      }
      finally{
     setLoading(false);
       }
    }
  };
};

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



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const UserProfile = ({ id }) =&amp;gt; {
  const { response, loading, fetch, isMounted } = useFetch(getUserProfile, id);
  useEffect(() =&amp;gt; {
    fetch();
    return () =&amp;gt; {
      isMounted.current = false;
    };
  }, []);
  return (
    &amp;lt;&amp;gt;{loading ? &amp;lt;p&amp;gt;Loading...&amp;lt;/p&amp;gt; : response &amp;amp;&amp;amp; &amp;lt;p&amp;gt;{response.userName}&amp;lt;/p&amp;gt;}&amp;lt;/&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;isMounted&lt;/code&gt; is used to detect component &lt;code&gt;unmount&lt;/code&gt; for not firing unnecessary state update.&lt;br&gt;
hope you like it,thanks. &lt;/p&gt;

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