{"path": "/posts/redux-e-usereducer/", "title": "Redux e useReducer · Bruno Dias", "html": "<!doctype html>\n<html lang=\"en\">\n<head>\n  <meta charset=\"utf-8\">\n  <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n  <meta name=\"description\" content=\"Prefer using just useReducer...\">\n  <title>Redux e useReducer · Bruno Dias</title>\n  <link rel=\"canonical\" href=\"https://diasbruno.github.io/posts/redux-e-usereducer/\" data-post-metadata>\n  <meta name=\"author\" content=\"Bruno Dias\" data-post-metadata>\n  <meta property=\"og:type\" content=\"article\" data-post-metadata>\n  <meta property=\"og:title\" content=\"Redux e useReducer\" data-post-metadata>\n  <meta property=\"og:url\" content=\"https://diasbruno.github.io/posts/redux-e-usereducer/\" data-post-metadata>\n  <meta property=\"article:published_time\" content=\"2025-10-31T01:51:29+00:00\" data-post-metadata>\n  <meta property=\"og:image\" content=\"https://diasbruno.github.io/assets/site/substack/redux-e-usereducer/figure-1.jpg\" data-post-metadata>\n  <link rel=\"icon\" href=\"/assets/site/signature.svg\" type=\"image/svg+xml\">\n  <link rel=\"alternate\" type=\"application/atom+xml\" title=\"diasbruno\" href=\"/atom.xml\">\n  <link rel=\"alternate\" type=\"application/json\" href=\"/posts/redux-e-usereducer/content.json\">\n  <link rel=\"stylesheet\" href=\"/assets/site/tokens.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/site.css\">\n  <link rel=\"stylesheet\" href=\"/assets/site/footer.css\">\n\n</head>\n<body class=\"light\">\n<div id=\"site-page\">\n<a class=\"skip-link\" href=\"#content\">Skip to content</a>\n<header class=\"site-header\">\n  <a class=\"brand\" href=\"/\" data-site-navigation aria-label=\"Bruno Dias, home\">\n    <img src=\"/assets/site/signature.svg\" width=\"24\" height=\"34\" alt=\"\">\n  </a>\n  <nav aria-label=\"Main navigation\">\n    <ul>\n<li><a class=\"nav-vrn\" href=\"/vrn/\" data-site-navigation><span class=\"nav-label\">vrn</span> <span class=\"nav-badge\">research</span></a></li>\n<li><a class=\"nav-posts\" href=\"/posts/\" data-site-navigation aria-current=\"location\">Posts</a></li>\n<li><a class=\"nav-projects\" href=\"/projects/\" data-site-navigation>Projects</a></li>\n<li><a class=\"nav-about\" href=\"/about/\" data-site-navigation>About</a></li>\n</ul>\n  </nav>\n</header>\n\n<main class=\"section\" id=\"content\" tabindex=\"-1\">\n  <article class=\"post-detail\">\n    <header class=\"section-header\">\n      <p class=\"post-category\">Post</p>\n      <h1>Redux e useReducer</h1>\n<p class=\"post-subtitle\">Prefer using just useReducer...</p>\n      <p><time datetime=\"2025-10-31T01:51:29+00:00\">2025-10-31</time></p>\n    </header>\n    <div class=\"post-columns\">\n      <section class=\"post-body\" aria-label=\"Article\">\n\n<h2>Idea</h2>\n<p>Both have the same origin. The idea consists of:</p>\n<p><strong>“The resultant state of the application is the application of its event history to a clean initial state.”</strong></p>\n<p>This is what our old <strong>reduce</strong> function does:</p>\n<pre><code>reducer :: ((B, A) -&gt; B) -&gt; B -&gt; T A -&gt; B</code></pre>\n<blockquote><p>OBS: This is a modified signature to met the javascript. The way Haskell implements is “<em><strong>foldl :: (b -&gt; a -&gt; b) -&gt; b -&gt; t a -&gt; b</strong></em>“</p>\n</blockquote>\n<p>Having a initial state  <strong>B</strong>, we can apply all the events that a container<strong> T A has </strong>(in typescript it could be something like <strong>A[] </strong>where<strong> T is the [] - any container</strong>), and, in the end, we have the resultant state. <strong>(B, A) -&gt; B</strong> is the reduction function. It’s the function that knows how to combine each event to the state.</p>\n<p>In javascript if would be like:</p>\n<pre><code>[eventA, eventB].reducer(\n  (currentState, event) =&gt; currentState + event,\n  state\n)</code></pre>\n<h2>How does it work?</h2>\n<p>It’s just a “loop”:</p>\n<figure><img src=\"/assets/site/substack/redux-e-usereducer/figure-1.jpg\" alt=\"UI event handlers dispatch events to the store reducer, which sends the current state back to the UI.\" loading=\"lazy\" width=\"1000\" height=\"300\">\n</figure>\n<p>The reducer provides a pair of <strong>[state, dispatcherFunction]. </strong></p>\n<p>The state at some time and a dispatcher function that, once called, it trigger the rebuild of the react components’ tree.</p>\n<p>You can make a stateful dispatch function pretty easily.</p>\n<pre><code>let rendered, state;\n\nfunction render() {\n  function dispatcher(event) {\n    state = reducer(state, event)\n    render();\n  }\n  rendered = vdom_patch(rendered, app(state, dispatcher));\n}</code></pre>\n<p>The UI <strong>event handler</strong>  <strong>dispatches</strong> an <strong>event </strong>that will be combined and<strong> stored</strong> for the next rendering.</p>\n<p>A difference from using useReducer and Redux is that Redux has the same problem of many javascript libraries - it only deals with primitive types.</p>\n<h2>Redux “bad practice”</h2>\n<p>It’s common to view an application as a hierarchy of components creating levels. It’s a difficult to avoid mental model.</p>\n<p>This way we have to pass the state on each level of the tree, so that all the components have access to it.</p>\n<p>This is what people call <strong>property drilling</strong>.</p>\n<figure><img src=\"/assets/site/substack/redux-e-usereducer/figure-2.png\" alt=\"State passes from the store through the router and page to the components.\" loading=\"lazy\" width=\"1000\" height=\"300\">\n</figure>\n<p>Contrary to what people say, there is no problem with that. The information must flow somehow. Property drilling is even better than what people recommend <strong>to connect every component directly to the store</strong>, or get the most stateful labrary.</p>\n<figure><img src=\"/assets/site/substack/redux-e-usereducer/figure-3.png\" alt=\"The components connect directly to the store instead of receiving state from the page.\" loading=\"lazy\" width=\"1000\" height=\"300\">\n</figure>\n<h2>ViewController (nothing new here)</h2>\n<p>This component is responsible for managing the state, passing state to other components, and provides functionality for them.</p>\n<p>It’s one strategy to deal with coupling simple components and state.</p>\n<p>For convenience, using redux, we can connect a store to this component, and, it will respond to the route.</p>\n<p><strong>Page</strong> is just any layout. It just knows how to place items in their right place.</p>\n<p><strong>Components </strong>that would be hidden inside the page, we bring them close to the state.</p>\n<p>This way, we can manage independent components of the store, and it will also improve the way we write our tests by reducing the amount of dependencies (and terrible mocks).</p>\n<figure><img src=\"/assets/site/substack/redux-e-usereducer/figure-4.png\" alt=\"The view controller connects to the store and passes state to the components that make up the page.\" loading=\"lazy\" width=\"1001\" height=\"400\">\n</figure>\n\n      </section>\n      <aside class=\"post-links\" aria-labelledby=\"post-links-heading\">\n        <h2 id=\"post-links-heading\">Links</h2>\n        <ul>\n<li><a href=\"/posts/\" data-site-navigation>All posts</a></li>\n<li><a href=\"https://diasbruno.substack.com/p/redux-e-usereducer\">Original on Substack</a></li>\n        </ul>\n      </aside>\n    </div>\n  </article>\n</main>\n\n<footer class=\"site-footer\">\n  <div class=\"site-footer-inner\">\n    <a class=\"site-footer-home\" href=\"/\" data-site-navigation>Bruno Dias</a>\n    <ul aria-label=\"Footer links\">\n      <li><a href=\"https://github.com/diasbruno\">GitHub</a></li>\n      <li><a href=\"/atom.xml\">Atom feed</a></li>\n    </ul>\n  </div>\n</footer>\n\n</div>\n<script type=\"module\" src=\"/assets/site/navigation.js\"></script>\n</body>\n</html>\n"}
