{"data":{"site":{"siteMetadata":{"title":"lastrites2018","author":"lastrites2018","siteUrl":"https://lastrites2018.netlify.com","comment":{"disqusShortName":"","utterances":"lastrites2018/gatsby-first-blog"},"sponsor":{"buyMeACoffeeId":""}}},"markdownRemark":{"id":"e1f83827-981e-57d6-b50b-b776192dba28","excerpt":"checkpoint 11 리덕스 설치 veloport redux 지니는 방 자체를 새로 만들어서, 방을 바꿔 버립니다. 이런 방식이 redux가 사용하는 방법. flux 아키텍쳐를 구현한 라이브러리. 컴퍼넌트 및 데이터 교류를 쉽고 효율적으로 하게 함. flux는 아이디어. 그냥 추상적인 개념이고, 이걸 구현한게 redux. mvc…","html":"<h2 id=\"checkpoint-11\"><a href=\"#checkpoint-11\" aria-label=\"checkpoint 11 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>checkpoint 11</h2>\n<div class=\"gatsby-highlight\" data-language=\"react\"><pre class=\"language-react\"><code class=\"language-react\">1. 리액트는 프레임워크가 아니다. 라이브러리다.\n리액트는 two-way data flow가 아니라 one way flow다.\n리덕스가 flux 라이브러리\n프레임워크랑 라이브러리 차이 알아보기!\n\n2. 버츄얼 DOM은 새로운 부분만 렌더링 해줌. 리액트의 큰 특징 중 하나. 리액트의 장점 : 버츄얼 DOM을 써서 빠르다! component reRender를 효과적으로 할 수 있게 해줌.\n\n3. DOM을 잘 바꿀 수 있게 해줌.\n\n4. props 부모 컴퍼넌트로부터 자식 컴퍼넌트로 속성을 내려주는데 사용, state는 컴퍼넌트의 현재 상태를 나타냄. 라이프 사이클을 통해서 액션을 바꿔줌. 컴퓨터는 상태기계. 상태를 조절하는 것이 컴퍼넌트. 어떤 때 pros를 쓰고, 어떤때 스테이터스를 써야 하는지 알아야 한다.\n\n5. import, export 같은 경우를 es5로 바꿔줌. Babel은 컴파일러지, 번들러는 아님. 3가지 차이점(컴파일러, 트랜스파일러, 번들러의 차이를 알아야 함. 공식 문서를 읽어야 함.)\n\nRedux의 정의(3번) : Redux is the name of a Flux library that is used to manage state in a React application.\n\nAjax는 native로 짤 수도 있고, 진화된 형태의 fatch api도 있음.\n\n6. state에 대한 설명\nState should be held at the top-most component that needs access to it\n상태는 꼭 상위에 있을 필요는 없다. 자체내에서 관리만 할 수 있다면. state 관리가 리액트의 난제이자 숙제이고, 그걸 해결해보겠다는게 리덕스.\nComponents manage their own state.\nInitial values can be set for state\n\nState can be passed from parent components to child components as props\n\n7. functional도 내부적으로 렌더 됨. return은 항상 있어야 함.\nreturn도 렌더와 동일.\narray 형태로 return이 됨. multiple 형태로 return이 됨. 이건 최근 변경상황.\n소괄호하는 것은 사실 return(return statement를 생략한 es2016 문법)\n\n* 모든 컴퍼넌트는 엘리먼트를 리턴함.\n스테이트풀하다 -&gt; 클래스로 만든 것\nstate는 순수 오브젝트.\n\n9. 1번 어느 정도는 동의하지만 리덕스에 비하면 별로라는 의미로?\n    프레임 워크가 아니다. 모든 유틸리티를 제공하지 않는다.\n\n11. didmount?\n    언제 데이터가 들어올지 모르니까!\n    라이프 싸이클은 자체적으로 돌아가고.\n\trender에 사이트 이펙트가 생길만한 것을 넣으면 안됨.\n\t다 mount 된 이후에 state가 안전하게 셋할 수 있는 환경을 만들어야.\n    생각해볼 문제.\n\n12. Define a function that updates state on component A, pass the function as a prop to component B, and invoke the function when B is clicked on.</code></pre></div>\n<h2 id=\"리덕스-설치\"><a href=\"#%EB%A6%AC%EB%8D%95%EC%8A%A4-%EC%84%A4%EC%B9%98\" aria-label=\"리덕스 설치 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>리덕스 설치</h2>\n<p><code class=\"language-text\">npm i -S redux</code></p>\n<h2 id=\"veloport-redux\"><a href=\"#veloport-redux\" aria-label=\"veloport redux permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>veloport redux</h2>\n<p>지니는 방 자체를 새로 만들어서, 방을 바꿔 버립니다. 이런 방식이 redux가 사용하는 방법. flux 아키텍쳐를 구현한 라이브러리. 컴퍼넌트 및 데이터 교류를 쉽고 효율적으로 하게 함.</p>\n<p>flux는 아이디어. 그냥 추상적인 개념이고, 이걸 구현한게 redux.</p>\n<p>mvc 패턴은 컨트롤러 -> 모델 -> 뷰</p>\n<p>모델 뷰의 구조가 엄청 복잡함.</p>\n<p>채팅창이 여러개 있는데, 동시에 채팅을 보여주는 모든 컴퍼넌트가 다 렌더링되어야 함. 페이스북에선 기존 문제로는 해결할 수가 없었음.</p>\n<p>ACTION -> DISPATCHER -> STORE -> VIEW</p>\n<p>시스템이 어떤 액션을 받았을때, 디스패쳐가 받은 액션이 있다면 스토어에 전달하고 변동된 상황이 있으면 뷰에 렌더링. 단, 뷰에선 스토어에 접근하지 않음! 뷰에서 디스패처로 액션을 보내고, 디스패쳐에선 중첩되지 않게 함.</p>\n<p>어떤 액션이 디스패치를 통하여 스토어에 있는 데이터를 처리하고</p>\n<p><a href=\"http://bestalign.github.io/2015/10/06/cartoon-guide-to-flux/cd\">http://bestalign.github.io/2015/10/06/cartoon-guide-to-flux/cd</a></p>\n<hr>\n<h2 id=\"세상에서-가장-쉬운-리덕스-따라하기\"><a href=\"#%EC%84%B8%EC%83%81%EC%97%90%EC%84%9C-%EA%B0%80%EC%9E%A5-%EC%89%AC%EC%9A%B4-%EB%A6%AC%EB%8D%95%EC%8A%A4-%EB%94%B0%EB%9D%BC%ED%95%98%EA%B8%B0\" aria-label=\"세상에서 가장 쉬운 리덕스 따라하기 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>세상에서 가장 쉬운 리덕스 따라하기</h2>\n<p>리덕스 원칙</p>\n<ol>\n<li>하나의 애플리케이션은 하나의 스토어만을 사용한다.</li>\n<li>state를 바꾸기 위한 유일한 방법은 action을 사용하는 것.</li>\n</ol>\n<div class=\"gatsby-highlight\" data-language=\"react\"><pre class=\"language-react\"><code class=\"language-react\">{\n    type : &quot;WITHDRAW_MONEY&quot;,\n    amount : &quot;$10,000&quot;\n}</code></pre></div>\n<p>state를 업데이트 하거나 바꾸기 위해서는, need to dispatch an action.</p>\n<ol start=\"3\">\n<li>To specify how the state tree is transformed by actions, you write pure reducers.</li>\n</ol>\n<p>스토어, 리듀서, 액션이 어떻게 작동하는지 이해해야 한다.</p>\n<h2 id=\"dispatch란\"><a href=\"#dispatch%EB%9E%80\" aria-label=\"dispatch란 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>dispatch란</h2>\n<p>리덕스 세상에서는 action을 reducer에게 전달하는 것을 의미한다. 리듀서는 뭘 해야 할 지 알고 있고, 늘 항상 새로운 state를 반환한다</p>\n<h2 id=\"hello-world-리팩토링\"><a href=\"#hello-world-%EB%A6%AC%ED%8C%A9%ED%86%A0%EB%A7%81\" aria-label=\"hello world 리팩토링 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>hello world 리팩토링</h2>\n<p>react에서 리덕스를 사용할때 첫번째 컴포넌트 리팩토링은 내부에서 구성 요소를 제거하는 것.</p>\n<h2 id=\"reducers\"><a href=\"#reducers\" aria-label=\"reducers permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reducers?</h2>\n<p>리듀스는 리덕스에서 가장 중요한 개념. 감속기. 뭔가를 줄이는 개념이 맞다. JS의 array.reduce처럼 단지 기능일뿐이다. 두 개의 매개 변수를 취하는 함수이며, 첫 번째는 STATE, 두번째는 ACTION이다. 그렇다면 어디서 리듀서에게 스테이트와 액션을 전달하는지 궁금할 것. createStore(reducer)에서 전달한다.</p>\n<div class=\"gatsby-highlight\" data-language=\"react\"><pre class=\"language-react\"><code class=\"language-react\">function createStore(reducer) {\n    var state;\n    var listeners = []\n\n    function getState() {\n        return state\n    }\n\n    function subscribe(listener) {\n        listeners.push(listener)\n        return unsubscribe() {\n            var index = listeners.indexOf(listener)\n            listeners.splice(index, 1)\n        }\n    }\n\n    function dispatch(action) {\n        state = reducer(state, action)\n        listeners.forEach(listener =&gt; listener())\n    }\n\n    dispatch({})\n\n    return { dispatch, subscribe, getState }\n}</code></pre></div>\n<h2 id=\"리덕스를-생성하기-위한-최소한의-코드\"><a href=\"#%EB%A6%AC%EB%8D%95%EC%8A%A4%EB%A5%BC-%EC%83%9D%EC%84%B1%ED%95%98%EA%B8%B0-%EC%9C%84%ED%95%9C-%EC%B5%9C%EC%86%8C%ED%95%9C%EC%9D%98-%EC%BD%94%EB%93%9C\" aria-label=\"리덕스를 생성하기 위한 최소한의 코드 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>리덕스를 생성하기 위한 최소한의 코드</h2>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">import</span> <span class=\"token punctuation\">{</span> createStore <span class=\"token punctuation\">}</span> <span class=\"token keyword\">from</span> <span class=\"token string\">'redux'</span>\n<span class=\"token keyword\">const</span> store <span class=\"token operator\">=</span> <span class=\"token function\">createStore</span><span class=\"token punctuation\">(</span>reducer<span class=\"token punctuation\">)</span> <span class=\"token comment\">//this has been updated to include the created reducer.</span></code></pre></div>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">## reducer 코드\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token punctuation\">(</span>state<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">return</span> state\n<span class=\"token punctuation\">}</span>  <span class=\"token comment\">// 항상 new state를 반환한다.</span></code></pre></div>\n<h2 id=\"리덕스를-사용하는-순간-state는-단-하나의-redux-store에-관리되기를-원한다는-것\"><a href=\"#%EB%A6%AC%EB%8D%95%EC%8A%A4%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EC%88%9C%EA%B0%84-state%EB%8A%94-%EB%8B%A8-%ED%95%98%EB%82%98%EC%9D%98-redux-store%EC%97%90-%EA%B4%80%EB%A6%AC%EB%90%98%EA%B8%B0%EB%A5%BC-%EC%9B%90%ED%95%9C%EB%8B%A4%EB%8A%94-%EA%B2%83\" aria-label=\"리덕스를 사용하는 순간 state는 단 하나의 redux store에 관리되기를 원한다는 것 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>리덕스를 사용하는 순간, state는 단 하나의 REDUX STORE에 관리되기를 원한다는 것.</h2>\n<p>그래서 그렇게 바꿔줘야 하는 거. 기존 state 데이터는 몽땅 store에 넣는 것이 원칙. (로컬 state를 좀 사용해도 괜찮긴 하다.)</p>\n<ul>\n<li>\n<p>Redux is a predictable state container for JavaScript apps.</p>\n</li>\n<li>\n<p>The createStore factory function from Redux is used to create a Redux STORE</p>\n</li>\n<li>\n<p>The Reducer is the only mandatory argument passed into createStore()</p>\n</li>\n<li>\n<p>A REDUCER is just a function. A function that takes in two parameters. The first being the STATE of the app, and the other being an ACTION</p>\n</li>\n<li>\n<p>A Reducer always returns the NEW STATE of your application.</p>\n</li>\n<li>\n<p>The INITIAL STATE of your application, initialState is the second argument passed into the createStore function call.</p>\n</li>\n<li>\n<p>Store.getState() will return the current state of your application. Where Store is a valid Redux STORE.</p>\n</li>\n</ul>\n<h2 id=\"state를-업데이트하는-유일한-방법\"><a href=\"#state%EB%A5%BC-%EC%97%85%EB%8D%B0%EC%9D%B4%ED%8A%B8%ED%95%98%EB%8A%94-%EC%9C%A0%EC%9D%BC%ED%95%9C-%EB%B0%A9%EB%B2%95\" aria-label=\"state를 업데이트하는 유일한 방법 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>state를 업데이트하는 유일한 방법</h2>\n<p>setState()와 달리 리덕스는 state를 업데이트 하기 위한 유일한 방법은 REDUCER에 의도를 알리는 것이다. ACTION을 넘기는 방법인 <code class=\"language-text\">dispatching actions</code>로. Action은 반드시 <code class=\"language-text\">type</code> field가 필요하다. 이 필드가 액션의 의도를 설명하기 때문이다.</p>\n<p>일반적인 패턴은</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token punctuation\">{</span>\n    type <span class=\"token punctuation\">:</span> <span class=\"token string\">\"\"</span><span class=\"token punctuation\">,</span>\n    payload <span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>ex)</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token punctuation\">{</span>\n    type <span class=\"token punctuation\">:</span> <span class=\"token string\">\"withdraw_money\"</span><span class=\"token punctuation\">,</span>\n    payload <span class=\"token punctuation\">:</span> <span class=\"token punctuation\">{</span>\n       amount <span class=\"token punctuation\">:</span> <span class=\"token string\">\"$4000\"</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>type은 작업을 설명\npayload는 정보를 넣는 객체</p>\n<h2 id=\"reducer\"><a href=\"#reducer\" aria-label=\"reducer permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>reducer</h2>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">function</span> <span class=\"token function\">reducer</span><span class=\"token punctuation\">(</span>state<span class=\"token punctuation\">,</span> action<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">//return new state</span>\n<span class=\"token punctuation\">}</span>\n\n일반적인 패턴은\n\n<span class=\"token keyword\">function</span> <span class=\"token function\">reducer</span><span class=\"token punctuation\">(</span>state<span class=\"token punctuation\">,</span> action<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">switch</span> <span class=\"token punctuation\">(</span>action<span class=\"token punctuation\">.</span>type<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n        <span class=\"token keyword\">case</span> <span class=\"token string\">\"withdraw_money\"</span><span class=\"token punctuation\">:</span>\n            <span class=\"token comment\">//do something</span>\n        \t<span class=\"token keyword\">break</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">case</span> <span class=\"token string\">\"deposit-money\"</span><span class=\"token punctuation\">:</span>\n            <span class=\"token comment\">//do something</span>\n            <span class=\"token keyword\">break</span><span class=\"token punctuation\">;</span>\n        <span class=\"token keyword\">default</span> <span class=\"token punctuation\">:</span>\n            \t<span class=\"token keyword\">return</span> state<span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p><strong>다시 기억하자. 리덕스에서는 state object를 관리하기 위해 setState()를 사용할 수 없다.</strong></p>\n<p>리덕스 앱에서는 모든 작업이 reducer를 통과한다. 액션을 구분하기 위해 action.type을 지정하여 사용한다.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">function</span> <span class=\"token function\">reducer</span><span class=\"token punctuation\">(</span>state<span class=\"token punctuation\">,</span> action<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">switch</span> <span class=\"token punctuation\">(</span>action<span class=\"token punctuation\">.</span>type<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">case</span> <span class=\"token string\">'is_open'</span><span class=\"token punctuation\">:</span>\n      <span class=\"token keyword\">return</span> <span class=\"token comment\">//return new state</span>\n    <span class=\"token keyword\">case</span> <span class=\"token string\">'is_clicked'</span><span class=\"token punctuation\">:</span>\n      <span class=\"token keyword\">return</span> <span class=\"token comment\">//return new state</span>\n    <span class=\"token keyword\">default</span><span class=\"token punctuation\">:</span>\n      <span class=\"token keyword\">return</span> state\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h2 id=\"action-creator\"><a href=\"#action-creator\" aria-label=\"action creator permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>action creator</h2>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">export</span> <span class=\"token keyword\">function</span> <span class=\"token function\">setTechnology</span><span class=\"token punctuation\">(</span>text<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span>\n    type<span class=\"token punctuation\">:</span> <span class=\"token string\">'SET_TECHNOLOGY'</span><span class=\"token punctuation\">,</span>\n    tech<span class=\"token punctuation\">:</span> text<span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>es6 ver.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">SetTechnology</span> <span class=\"token operator\">=</span> text <span class=\"token operator\">=></span> <span class=\"token punctuation\">(</span><span class=\"token punctuation\">{</span> type<span class=\"token punctuation\">:</span> <span class=\"token string\">'SET_TECHNOLOGY'</span><span class=\"token punctuation\">,</span> text <span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span></code></pre></div>\n<h2 id=\"redux의-폴더-컨벤션\"><a href=\"#redux%EC%9D%98-%ED%8F%B4%EB%8D%94-%EC%BB%A8%EB%B2%A4%EC%85%98\" aria-label=\"redux의 폴더 컨벤션 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>redux의 폴더 컨벤션</h2>\n<p><code class=\"language-text\">actions</code>, <code class=\"language-text\">reducers</code> , <code class=\"language-text\">store</code> 폴더를 만들어서 각각 저장하는게 일반적.</p>\n<h2 id=\"reducer에서-리턴된-값을-절대로-변경하면-안-된다\"><a href=\"#reducer%EC%97%90%EC%84%9C-%EB%A6%AC%ED%84%B4%EB%90%9C-%EA%B0%92%EC%9D%84-%EC%A0%88%EB%8C%80%EB%A1%9C-%EB%B3%80%EA%B2%BD%ED%95%98%EB%A9%B4-%EC%95%88-%EB%90%9C%EB%8B%A4\" aria-label=\"reducer에서 리턴된 값을 절대로 변경하면 안 된다 permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>reducer에서 리턴된 값을 절대로 변경하면 안 된다</h2>\n<p>항상 새로운 state를 반환해야 함.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\"><span class=\"token comment\">// 아래처럼 하면 절대 안 됨!</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">default</span> <span class=\"token punctuation\">(</span>state<span class=\"token punctuation\">,</span> action<span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">switch</span> <span class=\"token punctuation\">(</span>action<span class=\"token punctuation\">.</span>type<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">case</span> <span class=\"token string\">'SET_TECHNOLOGY'</span><span class=\"token punctuation\">:</span>\n      state<span class=\"token punctuation\">.</span>tech <span class=\"token operator\">=</span> action<span class=\"token punctuation\">.</span>text\n      <span class=\"token keyword\">return</span> state\n\n    <span class=\"token keyword\">default</span><span class=\"token punctuation\">:</span>\n      <span class=\"token keyword\">return</span> state\n  <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// 대신에</span>\n<span class=\"token keyword\">return</span> <span class=\"token punctuation\">{</span>\n  <span class=\"token operator\">...</span>state<span class=\"token punctuation\">,</span> <span class=\"token comment\">// 이렇게!</span>\n  tech<span class=\"token punctuation\">:</span> action<span class=\"token punctuation\">.</span>text<span class=\"token punctuation\">,</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>이렇게 해야 한다. 상태를 변경하는 대신\n새로운 객체를 반환해야 한다.</p>","frontmatter":{"title":"Immersive Sprint - REDUX","date":"October 09, 2018"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/til/20181008-20181009 IM08D15-16 REDUX/","previous":{"fields":{"slug":"/til/20181004-1006 IM08D12-14 REACT/"},"frontmatter":{"title":"Immersive Sprint - REACT","category":"TIL"}},"next":{"fields":{"slug":"/til/20181010-20181011 IM08D17-18 NODE JS/"},"frontmatter":{"title":"Immersive Sprint - NODE.js","category":"TIL"}}}}