<?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: Jae Park</title>
    <description>The latest articles on DEV Community by Jae Park (@jaebungs).</description>
    <link>https://dev.to/jaebungs</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%2F573822%2Fc1ab92a5-edbb-464d-a6c7-706a729a1aa3.jpeg</url>
      <title>DEV Community: Jae Park</title>
      <link>https://dev.to/jaebungs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jaebungs"/>
    <language>en</language>
    <item>
      <title>Why Redux-thunk? </title>
      <dc:creator>Jae Park</dc:creator>
      <pubDate>Fri, 05 Feb 2021 02:55:13 +0000</pubDate>
      <link>https://dev.to/jaebungs/why-redux-thunk-1dka</link>
      <guid>https://dev.to/jaebungs/why-redux-thunk-1dka</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: I am writing this to organize my thought and keep some records. Please let me know if something is wrong or have a  better explanation. Thank you!&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;If you are app does not interact with a server or use API call, you wouldn't need redux-thunk because you don't need async actions. &lt;br&gt;
Problems come when any server-side(REST API) is involved.&lt;/p&gt;


&lt;h4&gt;
  
  
  Why problems?
&lt;/h4&gt;

&lt;p&gt;Redux store only supports synchronous. See Redux documentation below.&lt;br&gt;
&lt;em&gt;By itself, a Redux store doesn't know anything about async logic. It only knows how to synchronously dispatch actions, update the state... ([&lt;a href="https://redux.js.org/tutorials/fundamentals/part-6-async-logic%5D"&gt;https://redux.js.org/tutorials/fundamentals/part-6-async-logic]&lt;/a&gt;)&lt;/em&gt;&lt;/p&gt;


&lt;h4&gt;
  
  
  Can I use API call in a reducer?
&lt;/h4&gt;

&lt;p&gt;No. Reducer is a pure function. It should not cause side effects and it should not directly mutate the state. Anyway, mutating state is an ah oh… in React. &lt;br&gt;
Redux uses action and reducer to update your app's state. By using these two, people can easily tell how data flow and when your state is changing.&lt;br&gt;
Reducer should copy the state first, then overwrite the value you want to change in the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return {
  ...state,
  zip: M0R0O0
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  The solution is here!
&lt;/h4&gt;

&lt;p&gt;Simply saying Redux-thunk is a middleware that allows users to use asynchronous functions when API calls are necessary.&lt;br&gt;
As the document said dispatching action happens immediately, but redux-thunk can make a delay or apply a condition.&lt;/p&gt;

&lt;p&gt;'Action' is an object, so 'action creator' should return the action object. Redux-thunk allows an action creator to return a function! Now we can do any asynchronous work.&lt;/p&gt;

&lt;p&gt;Let's see how thunk works in code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const addToMyBar = (cocktail, id) =&amp;gt; async (dispatch) =&amp;gt; {
    await fetch('http://localhost:5000/', {
    })
    .then((res)=&amp;gt;res.json())
    .then((data)=&amp;gt; {
        dispatch({type: 'ADD_TO_MY_BAR', data});
    })
    .catch(err =&amp;gt; console.log(err))
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  How to use?
&lt;/h4&gt;

&lt;p&gt;Install the package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save redux-thunk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore, combineReducers, applyMiddleware, compose  } from "redux";
import thunk from 'redux-thunk';

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

&lt;/div&gt;



&lt;p&gt;Example )&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default () =&amp;gt; {
  const store = createStore(
    combineReducers({
      cocktails: cocktailsReducers,
      myBar: myBarReducer,
    }),
    compose(
      applyMiddleware(thunk),
      window.__REDUX_DEVTOOLS_EXTENSION__ &amp;amp;&amp;amp; 
      window.__REDUX_DEVTOOLS_EXTENSION__()
    )
  );
  return store;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thank you. &lt;br&gt;
I know this is not well unorganized...&lt;/p&gt;

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