{"id":424,"date":"2022-04-10T18:56:49","date_gmt":"2022-04-10T08:56:49","guid":{"rendered":"https:\/\/www.kernelio.com\/blog\/?p=424"},"modified":"2022-04-10T18:56:49","modified_gmt":"2022-04-10T08:56:49","slug":"flux-observer-factory-pattern-and-redux","status":"publish","type":"post","link":"https:\/\/www.kernelio.com\/blog\/2022\/04\/10\/flux-observer-factory-pattern-and-redux\/","title":{"rendered":"Flux, Observer, Factory pattern and Redux"},"content":{"rendered":"<p>Redux is an implementation of Flux pattern which aims to solve the state management issue in large scale complex react app.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Flux pattern<\/strong> at its core is pipe line:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol>\n<li>An action dispatches an event.<\/li>\n<li>Event is send at data store and triggers an mutation of the data.<\/li>\n<li>Then the data mutation triggers the view update<\/li>\n<li>User&#8217;s behaviour on view triggers more actions<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-425\" src=\"http:\/\/www.kernelio.com\/blog\/wp-content\/uploads\/2022\/04\/Flux-vs-MVC.jpeg\" alt=\"\" width=\"638\" height=\"479\" srcset=\"https:\/\/www.kernelio.com\/blog\/wp-content\/uploads\/2022\/04\/Flux-vs-MVC.jpeg 638w, https:\/\/www.kernelio.com\/blog\/wp-content\/uploads\/2022\/04\/Flux-vs-MVC-300x225.jpeg 300w\" sizes=\"auto, (max-width: 638px) 100vw, 638px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>React state updates triggers re-render of the view.\u00a0 When using Redux with React, React needs to know when state has changed in Redux. To make this happen Redux simply provides a\u00a0 subscribe() function to allow register event listener on state change. This is <strong>Observer pattern.<\/strong><\/p>\n<pre>componentDidMount() {\r\n  store.subscribe(() =&gt; this.forceUpdate());\r\n}<\/pre>\n<p>The above code bind Redux state change with React forceUpdate function.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>createStore(reducer, [initState], [enhancer])<\/strong> function can be considered as an implementation of <strong>Factory pattern. <\/strong>it takes<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>reducer function which describe how state should be mutated by events<\/li>\n<li>initial state<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>It returns a datastore object which provides<\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><strong>getState()<\/strong> function which returns the current state<\/li>\n<li><strong>dispatch(event) <\/strong>function witch triggers actions defined in reducer<\/li>\n<li><strong>subscribe(listener)<\/strong> function witch add a listener function on state change<\/li>\n<li><strong>replaceReducer(reducer)<\/strong> function which replace current reducer stored on datastore object with a new one.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Redux is an implementation of Flux pattern which aims to solve the state management issue in large scale complex react app. &nbsp; Flux pattern at its core is pipe line: An action dispatches an event. Event is send at data store and triggers an mutation of the data. Then the data mutation triggers the view&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-424","post","type-post","status-publish","format-standard","hentry","category-front-end"],"_links":{"self":[{"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/posts\/424","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":3,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kernelio.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}