<?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: Tatu Tamminen</title>
    <description>The latest articles on DEV Community by Tatu Tamminen (@ttamminen).</description>
    <link>https://dev.to/ttamminen</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%2F227682%2F3c2f1721-fbde-4f39-aada-30203dc9c113.png</url>
      <title>DEV Community: Tatu Tamminen</title>
      <link>https://dev.to/ttamminen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ttamminen"/>
    <language>en</language>
    <item>
      <title>React custom hook question</title>
      <dc:creator>Tatu Tamminen</dc:creator>
      <pubDate>Wed, 11 Sep 2019 12:32:23 +0000</pubDate>
      <link>https://dev.to/ttamminen/react-custom-hook-question-10me</link>
      <guid>https://dev.to/ttamminen/react-custom-hook-question-10me</guid>
      <description>&lt;p&gt;I have been implementing a React custom hook. In the project, I noticed a pattern when getting data to the React components.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;first, we check do we have data already in the store (selector in the function will give the data)&lt;/li&gt;
&lt;li&gt;if not, we dispatch an action that will have a token and parameters&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As it is a crucial part of the app, I thought someone with hook experience might have a look at this. &lt;/p&gt;

&lt;p&gt;Especially, the payload and action part. Should I use &lt;code&gt;usePrevious&lt;/code&gt; hook and use &lt;code&gt;isEqual&lt;/code&gt; from lodash. Or, &lt;code&gt;useMemo&lt;/code&gt;?.&lt;/p&gt;

&lt;p&gt;All ideas appreciated, thanks!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* annotated version of the hook */

export function useFetchIfNeeded&amp;lt;T&amp;gt;(
  selector: (state: AppState) =&amp;gt; T,
  action: (payload: any) =&amp;gt; ThunkAction&amp;lt;any, any, any, any&amp;gt;,
  payload: Object,
  checkFn?: (input: T) =&amp;gt; boolean
) {
  // redux hook
  const dispatch = useDispatch()

  // get token
  const token = useSelector((state: AppState) =&amp;gt; state.global.token)

  // get data using the selector function
  const data = useSelector(selector)

  // use check function to verify do we need to fetch data or not
  // default is null comparison
  const check = useCallback(() =&amp;gt; (checkFn ? checkFn(data) : data !== null), [
    checkFn,
    data
  ])
  const fetch = useCallback(() =&amp;gt; {
    if (!token) {
      return
    }

    if (check()) {
      return
    }

    dispatch(
      action({
        ...payload,
        token
      })
    )
    // TODO: action and payload are objects, should I make my own comparison?
  }, [dispatch, token, check, action, payload])
  useEffect(() =&amp;gt; {
    fetch()
  }, [fetch])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



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