<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Justin Dragos on Medium]]></title>
        <description><![CDATA[Stories by Justin Dragos on Medium]]></description>
        <link>https://medium.com/@ellisande_poet?source=rss-d3b633aafc67------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*9Wx_FI6F0SKy_DaR.</url>
            <title>Stories by Justin Dragos on Medium</title>
            <link>https://medium.com/@ellisande_poet?source=rss-d3b633aafc67------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:51:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ellisande_poet/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Subscriptions With React Hooks]]></title>
            <link>https://medium.com/@ellisande_poet/subscriptions-with-react-hooks-d329d473efc8?source=rss-d3b633aafc67------2</link>
            <guid isPermaLink="false">https://medium.com/p/d329d473efc8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[firebase]]></category>
            <dc:creator><![CDATA[Justin Dragos]]></dc:creator>
            <pubDate>Sat, 27 Oct 2018 22:32:45 GMT</pubDate>
            <atom:updated>2018-10-27T22:38:51.604Z</atom:updated>
            <content:encoded><![CDATA[<p>Setup and manage a data subscription using useState, useContext, and useEffect from React.</p><p>Warning:<br>*** This post was written one day after hooks were announced. If you are looking for production examples of hooks and best practices <strong>PLEASE DO NOT USE THIS ARTICLE </strong>***</p><p>I spent the weekend at React Conf 2018 where they just announced a new pattern for dealing with side effects the thing they are calling Hooks. I’m a huge fan of the Higher Order Component and Render Props patterns so I set out to re-write a component with lots of them using hooks instead. Here’s my adventure.</p><p><strong>What are hooks?</strong></p><p>Hooks are a way to manage side effects inside a functional component. They can be used to manage state, context, subscriptions, and almost anything else your component needs to do without needing to use extends React.Component or lifecycle methods.</p><p>The React team has done a better job of explaining the motivations and implementation of hooks than I ever could so I’ll defer to them for an explanation:</p><p><a href="https://youtu.be/dpw9EHDh2bM?t=927">Dan Abramov Introduces Hooks</a></p><p><a href="https://reactjs.org/docs/hooks-intro.html">React Hooks Documentation</a></p><p><strong>So can you do with hooks?</strong></p><p>The short answer is everything. They give functional components the same functionality you get from a class component, plus some brand new APIs that can really make your life a lot easier.</p><p><strong>What can’t I do with hooks?</strong></p><p>Here are some restrictions you should be aware of with the current implementation:</p><ul><li>Hooks can’t be used in class components. If you are extending React.Component you need to use the existing lifecycle methods.</li><li>Hooks can’t be conditional. You can use conditions <strong>inside</strong> a hook, but the hook itself can’t be conditional.</li><li>Hooks are order dependent. This sounds scary, but so far in practice, it doesn’t seem particularly impactful.</li><li>Hooks are only available in &gt; 16.7.0 which is currently in alpha.</li></ul><p><strong>So what are we going to do with hooks?</strong></p><p>I took a render prop component with a bunch of side effects and converted it all to hooks, I’m going to walk through what I did any show the end results. The component I’m refactoring gets real-time data updates from Firebase’s firestore database.</p><p><strong>A Note on How Firebase / Firestore Works</strong></p><p>This component is complex due to the way the Firestore APIs work. You need to start with an initialized Firebase App, then get a database from it and finally build a reference that points to the path where your data resides. The goal of this component is to abstract all the subscription and reference building from the consumer. Ideal usage is like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9mK9gos_6DX8-qgMixbmw.png" /><figcaption>Usage Pattern for the FirebaseQuery component</figcaption></figure><h4>Refactor to Hooks</h4><p>Alright, there is a lot going on in this component. Where should we start? The first thing that stuck out to me is that even though the component is used in the render prop style but it uses two HoCs withFirebaseApp and withDatabase. The reason? The two props app and database need to be used in lifecycle methods, so only having access to them in render won’t work.</p><p><strong>Step 1: Write the HoCs as Hooks</strong></p><p>Starting code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FjueLYT91a_aBNQ4QvanJA.png" /><figcaption>Starting Component HoCs</figcaption></figure><p>The withFirebaseApp HoC is just getting a prop from context and there is a first-class hook for that so let&#39;s try it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Y45umNaC4tqfzB45kKivPw.png" /><figcaption>New Component with UseContext hook</figcaption></figure><p>Ok, so I’m not really sure what’s going on underneath, but it looks like its giving us our app value from context. Great! Now we need to use app to get a reference to the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7_DYHZNatyqIt-5nixY6Zw.png" /><figcaption>New Component with database computed value</figcaption></figure><p><strong>useContext</strong></p><p><em>What you get:<br></em>The latest value from context</p><p><em>What React promises:<br></em>If the value in context changes your functional component will be re-rendered with the new value from context</p><p><em>What are the benefits:<br></em>Honestly not many over the existing patterns. It’s nice to be able to treat values from context in a similar fashion to props, but you can get the same pattern from an HoC (which literally does give you a prop).</p><p>This appears to be very straightforward, but there is some magic going on here. We are treating app like a value, but what React is promising from the hook is that any time the value of app changes in the context our function will re-render with the new value. This is very similar to how you use props in a render function. You don’t worry about the fact that props can change you only worry about the current value. React promises to watch for the updates and call render for you with the new props.</p><p><strong>Step 2: Migrate the State</strong></p><p>The constructor from the original component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LHuvbLjy3s1VAvV_pMkycQ.png" /><figcaption>Constructor from the original component</figcaption></figure><p>We’ve got three things in the component state right now the firestore reference, a function to unsubscribe and the actual value from the database. I suspected another hook is going to manage my subscription for me so I’m going to just keep the value in state using useState.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7vSbcTVyifMPJVEfgZdNlw.png" /><figcaption>New Component with useState hook</figcaption></figure><p><strong>useState</strong></p><p><em>What you get:<br></em>We get two things from the hook: the current value from state, and a function we can use to update that value.</p><p><em>What react promises:<br></em>Any time the update function is called with a new value your component will re-render with the new value being returned.</p><p><em>What are the benefits:<br></em>The biggest benefit here is the ability to easily break your state into pieces (if you want). Since we are dealing with just one value from the state at a time we don’t need to worry about merging the state object every time we set.</p><p>Now we’ve come to something that is pretty different from the traditional path. In a regular component, we would’ve used this.state = { ..initialState } to initialize and then this.setState() to update the state. What in the world is going on here?</p><p>The pattern is very similar to the withState HoC from Recompose. I’m a big fan of the pattern because it encourages modularity and reduces the need to merge state on every single update. If you have a complex state which has many dependent properties (e.g. an isLoading flag in state) you won’t see many of these benefits.</p><p><strong>Step 3: Migrate The Subscription Setup</strong></p><p>The next step I wanted to take was actually doing the subscription. In practice, this meant replacing the logic in componentDidMount for first time setup. It boils down to two important operations:</p><ol><li>Build the reference using database, rootPath and path</li><li>Subscribe to updates</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tckWVPOmXzG_34_YAtfobQ.png" /><figcaption>Original componentDidMount implementation</figcaption></figure><p>I hadn’t really used most hooks at all, but from watching the introduction presentation I had an idea that the useEffect hook was going to be good at managing the subscription. So my first stab was at using that.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aldPhoRC7P1HBE-Q-22lTg.png" /><figcaption>Using useEffect hook to start a subscription</figcaption></figure><p>There’s a lot to unpack here. I want to step through what’s going on:</p><ul><li>We are using a hook called useEffect which does something magic with side effects</li><li>We are providing a function that sets up a firebase subscription using onSnapshot</li><li>When onSnapshot gets called we make a invoke the setValue function from our useState hook to update our state.</li></ul><p><strong>useEffect</strong></p><p><em>What do you get:<br></em>A function that gets invoked when the component renders the first time</p><p><em>What does React promise:<br></em>That the function will be invoked each time the component is rendered</p><p><em>What are the benefits:<br></em>So far the big benefit here is that we are essentially getting componentDidMount inside a functional component. Additionally, we can use useEffect more than once and modularize setup code if we need to do more than one thing.</p><p>At this point in the refactor I wasn’t impressed. We have moved a little logic from componentDidMount into our functional component, but the setup isn’t the annoying part of managing a subscription. The ability to run a function exactly once is cool, but not life-changing.</p><p>But what if it didn’t run exactly once…</p><p><strong>Step 4: Migrate Re-Subscribe</strong></p><p>This is the most annoying part of the original component and the easiest to mess up. If app, database, rootpath, or path change that means we need to rebuild the reference and subscribe to the new one. If you are writing a subscription component for the first time this can be really easy to miss. If the props never change (like in your unit test) it will appear to work exactly the way you want, but when used in a real application you’ll find that you are getting stale or completely unrelated data.</p><p>There is also nuance here. The component needs to re-render when the value from the subscription changes, but unless the parts that <em>make up the subscription</em> change we shouldn’t resubscribe.</p><p>Here is the original componentDidUpdate implementation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*H62oL3um8apIiP2XJl6Cyw.png" /><figcaption>Original componentDidUpdate implementation</figcaption></figure><p>Here’s what I came up with using hooks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jHgyiqKZG_qNjtrEVjTdmg.png" /><figcaption>useEffect hook with dependent values</figcaption></figure><p><strong>useEffect dependencies</strong></p><p><em>What you get:<br></em>A function that executes conditionally when the dependencies you declared have changed.</p><p><em>What React promises:<br></em>If the reference for any of your dependencies has changed the component will re-render and the function you provided will be run again.</p><p><em>What are the benefits:<br></em>This completely replaces logic that would go in componentDidUpdate. Additionally, you are no longer duplicating logic between componenetDidMount and componentDidUpdate.</p><p>Ok, this blew my mind. I wasn’t quick or smart enough to pick up on it during Ryan Florence’s presentation, but this second argument to useEffect is amazing. You simply give it the variables you that feed into your effect and <strong>the function is re-run</strong> <strong>only if the dependent variables change</strong>. Whoa.</p><p>Not only does this mean we aren’t managing the first time setup and update logic separately, but the entire process of comparing the sub-set of props that make up our subscription key is completely removed. All with <strong>25 characters of code</strong>.</p><p><strong>Step 5: Migrate Unsubscribe</strong></p><p>In the original component, there are two places we need to unsubscribe to stop bad behavior.</p><p>The first was in componentWillUnmount to stop memory leaks. If your component continues to try to update after its unmounted we’re wasting cycles and holding onto memory we don’t need. This is bad, but if you don’t do it your component will appear to work fine.</p><p>The second is far more important. In componentDidUpdate once if we don’t unsubscribe from the old stream of data we will actually get updates to our state from <em>both places</em><strong><em>. </em></strong>This is going to cause great confusion to your users and the more times you subscription updates the more competing changes you’ll have.</p><p>It turns out that the onSnapshot method returns an unsubscribe function so we just have to return it from our useEffect function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6pbaVl1ZHWaS6RDukbxPmA.png" /><figcaption>Returning an unsubscribe method from useEffect</figcaption></figure><p><strong>useEffect cleanup</strong></p><p><em>What you get:<br></em>The ability to run a cleanup function for your side effect.</p><p><em>What react promises:<br></em>This cleanup code will run when the component unmounts or before any time the function you provided will be run again.</p><p><em>What are the benefits:<br></em>Your subscription and unsub logic is modularized and co-located. The cleanup only needs to be in one place. Ensuring cleanup will run before any re-run of the function removes a lot of overhead.</p><p>My initial thought here was to simply store the unsub method in state and then find another hook to match the behavior of componentWillUnmount, but it turns out you don’t need that at all. useEffect lets you return an unsubscribe method that it will call either when the component unmounts <strong>or when you dependent properties have changed.</strong></p><p>This is so big. Not only is my unsub logic in one place but it can no longer get out of sync with the dependencies of the subscribe method. I can be certain that both cleanup and sub will <strong>only happen when a dependent value changes.</strong> This removes 32 lines of componentDidMount, componentDidUpdate, and componentWillUnmount logic in exchange for just 3 line of code. Even better, it&#39;s far more reliable than the old code and we don’t have to store a reference to unsubscribe in our state.</p><p><strong>Step 6: Migrate render</strong></p><p>The old component just passed a render prop down to its child, let’s continue that pattern.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a8B14_eEHuY0lv7oojeiZA.png" /><figcaption>Completed new component</figcaption></figure><p>Why pass a child prop? Even with hooks I still want my presentational components to be separate from the thing that provides their data. It’s a personal preference.</p><p><strong>Step 7: Make it even smaller</strong></p><p>We’ve used the React provided hook so far, but it’s also possible to create your own. Why don’t we extract our hook implementation and create a custom hook?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3O_aCShI1XSW26O6JIftuA.png" /><figcaption>Custom useUpdatingValue hook</figcaption></figure><p>Now our actual component is just 2 lines. We can also export our useUpdatingValue hook and other components can directly consume it. If you prefer a container component you can use it, if you want to just consume the hook you can.</p><p><strong>Conclusion:</strong></p><p><em>The Good:</em></p><p>This is very early on for the hook APIs and I suspect we’ll see lots of changes over the coming months. My initial take is that these are incredibly powerful. The usage patterns are much cleaner than the existing HoC and Render Props patterns. They are highly composable. They provide much better abstractions than the lifecycle methods do, based around what you want to do instead of an arbitrary hook into the lifecycle.</p><p>I like it.</p><p><em>The Bad:</em></p><p>Hooks are a lot like context. They can be used as a magical way to get data into a component without exposing it on the props interface. The single best thing React has done for the front end community is to make the path of least resistance to create an interface in front of your front end components. I am not excited to see that go away.</p><p>On the same vein, I’m worried that this is going to encourage developers to tightly couple their presentational components with the data that powers them. This will lead to more brittle components that are harder to compose and reuse than the older style. You can certainly still create container components but I think how and why are going to be less clear.</p><p>Either way, I can’t wait to see the abstractions and patterns that come from developers using hooks in their components. Here’s to another year of being really excited about the future of react 🥂.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d329d473efc8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Isomorphic Redux]]></title>
            <link>https://medium.com/@ellisande_poet/isomorphic-redux-fdaadea13bb5?source=rss-d3b633aafc67------2</link>
            <guid isPermaLink="false">https://medium.com/p/fdaadea13bb5</guid>
            <category><![CDATA[isomorphic]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Justin Dragos]]></dc:creator>
            <pubDate>Thu, 27 Apr 2017 04:42:52 GMT</pubDate>
            <atom:updated>2017-04-27T04:42:52.031Z</atom:updated>
            <content:encoded><![CDATA[<h4>Abusing Redux: Blog 1</h4><p>If you used the same Redux store on both sides of a stack, could you get distributed state for free? That’s the question that I set out to answer. TL;DR; Yes. Yes you can, and it’s <em>beautiful</em>.</p><p><strong>Background: A Crazy Idea</strong></p><p>I wrote a prototype React app one day. It was a re-write of something I had written a year ago. I had only prototyped the front end, but if I wanted it to work I would real time distributed state (like a chat app). My motivation for side projects tends to flame out quickly so I wanted something that would be fast and easy.</p><p>That’s when I hatched a crazy idea, what if I just took the Redux store I had already created, and used it on the server as well? Then I should be able to just pass actions over the socket and everything should just sync up!</p><p>Sure it was a little silly (Redux on the server?) and maybe a little crazy (free state sync’ing?) but it turned out to be fantastic. I did the work to hook up one action, but instead of just that action working my entire app started distributing itself over sockets to as many users as I wanted.</p><p>Simple. Sync’d. Beautiful.</p><p><strong>Pre-Requisites</strong></p><p>Before reading this you’ll want to be familiar with Redux and Websockets. I recommend <a href="https://css-tricks.com/learning-react-redux/">Leveling Up With Redux</a> by Brad Westfall for Redux and <a href="https://www.html5rocks.com/en/tutorials/websockets/basics/">Introducing Websockets</a> over on HTML 5 Rocks for some Websocket context.</p><p><strong>Goal</strong></p><p>Create a functioning chat app using React/Redux, a node server, and web sockets.</p><p><strong>Setup</strong></p><p>There is some setup I won’t be covering in this post, but will happily provide a place to start from:</p><p><a href="https://github.com/Ellisande/epic-redux/tree/the-beginning">Ellisande/epic-redux</a></p><p>Be sure to use the branch the-beginning if you want to follow along from the start.</p><p>We’ll start with these things already built for us:</p><ul><li>A prototype chat app (React/Redux front end only)</li><li>A node server (web server using Express)</li><li>Web sockets setup on client and server (we’ll use Primus)</li></ul><p><strong>The Plan</strong></p><p>My lazy and ingenious plan? Make Redux do all the work for me, and just magically have everything work! Ok, so I was foolish enough to take that at face value, but you probably aren’t. Lets talk about how we’ll actually pull this off:</p><p><em>FrontEnd</em><strong><em>:</em></strong></p><ul><li>Create an action (data only, pure Redux style)</li><li>Send it over the socket</li></ul><p><em>BackEnd</em>:</p><ul><li>Receive the action</li><li>Dispatch it to the store</li><li>Send it back to the front end</li></ul><p><em>FrontEnd:</em></p><ul><li>Dispatch the action to the front end store</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*i6mss0d28Xa56Lv2vLtKOg.gif" /><figcaption>Animation of Data Flow</figcaption></figure><p>If the plan works out the front end store should now be exactly the same as the back end store, and all we had to do was send an action over the socket, all the hard work was done for us!</p><p><strong>Sounds Fishy…</strong></p><p>We have an idea on how to send the data around, but can we really trust the stores will synchronized with each other? It turns out well designed Redux stores can actually guarantee that. Heres why:</p><p><em>If</em>:</p><ul><li>Each action contains only data <strong>and</strong></li><li>Each reducer is a pure function of old state and action</li></ul><p><em>Then:</em></p><ul><li>The product of the old state and the same action must produce the same state</li></ul><p>Mathematically it looks like this <em>f(state, action) = newState</em></p><p>In other words as long as the stores start at the same place, and both get the same actions the states will identical. Pretty neat right?</p><p><strong>Execution</strong></p><p>Alright, lets get to the meat of this thing! My approach for this on the front end was a little unorthodox. Instead of writing middleware I wrote a wrapper around my socket that exposes one method: dispatch</p><p>This dispatch function has an identical signature to the one from Redux except that it sends the action over the network to the server store and <strong>not</strong> the front end store.</p><pre>/** ui/js/services/socket.js **/<br>/** dispatch() **/</pre><pre>const dispatch = action =&gt; {<br>  roomConnection.write(action);<br>};</pre><p>Thats it. Three lines of code. Receiving is just as simple, we’ll take whatever action we were sent and dispatch it to the front end store.</p><pre>/** ui/js/services/socket.js **/<br>/** connectToRoom() **/</pre><pre>connection.on(&#39;data&#39;, action =&gt; {<br>  return store.dispatch(action);<br>});</pre><p>We can now replace the old dispatch with the socketDispatch and you’re front end will be ready to go.</p><pre>/** ui/js/components/chat.jsx **/<br>import {dispatch as socketDispatch} from &#39;../services/socket&#39;;</pre><pre>const mapDispatchToProps = (dispatch) =&gt; ({<br>  sendMessage: (newMessage, userName) =&gt; socketDispatch(sendMessage(newMessage, userName)),<br>  addUser: userName =&gt; socketDispatch(addParticipant({id: userName, name: userName})),<br>  setUserId: userId =&gt; dispatch(joinChat({userId})),<br>});</pre><p><em>Note</em>: do not change the dispatch for setUserId. This does need to do a one time front end only dispatch to set your user id in the front-end store. There is a better way to solve this kind of issue, but we’ll address that in the next blog.</p><p><strong>Service Side Receiving</strong></p><p>Alright, on the server we should now be getting actions through our socket dispatch, so what do we do with them? First off we’ll need a server side store. We’re going to use the exact same createStore method we used on the front end.</p><pre>/** server/room.js **/<br>/** constructor() **/</pre><pre>import {createStore} from &#39;../shared/store/store&#39;;</pre><pre>this.store = createStore();</pre><p>See? Nothing special, we’ll add a bit of middleware here in a minute, but looks good for now! Next up lets add a method for dispatching to the room’s store.</p><pre>/** server/room.js **/<br>/** dispatch() **/</pre><pre>dispatch(action){<br>  this.store.dispatch(action);<br>}</pre><p>Finally lets consume the actions sent to us over the web socket by dispatching them to the room.</p><pre>/** server/room.js **/<br>/** addUser() **/</pre><pre>addUser(spark){<br>    this.addSpark(spark);<br>    spark.on(&#39;data&#39;, action =&gt; {<br>      this.dispatch(action);<br>    });<br> }</pre><p>Any time we get data over the socket, we’ll just assume that its an action and dispatch it to our store. If this is feeling familiar to you, theres a reason, you wrote almost the exact same code on the front end.</p><p><strong>The Final Piece</strong></p><p>All that is left now is to take anything dispatched to our server side store and toss it back to the front end. In this case middleware is strong pattern, because we are just blindly sending the action after each successful dispatch.</p><pre>/** server/room.js **/<br>/** constructor() **/</pre><pre>const sendActionsMiddlware = () =&gt; next =&gt; action =&gt; {<br> const result = next(action);<br> this.sparks.forEach(spark =&gt; spark.write(result));<br> return result;<br>};</pre><p>Here’s a break down of whats happening in case you aren’t familiar with Redux middleware:</p><ol><li>Let the action go to the store next(action)</li><li>Send the action over the network to each connection this.sparks.forEach(spark.write(result))</li><li>Continue on with the rest of the middleware return result</li></ol><p>The last thing we need to do is add that middleware into our server store. Lets change the store creation to look like this:</p><pre>/** server/room.js **/<br>/** constructor() **/</pre><pre>import {applyMiddleware} from &#39;redux&#39;;</pre><pre>this.store = createStore(applyMiddleware(sendActionsMiddlware));</pre><p><strong>Feel the Magic</strong></p><p>Thats it! You should now be able to open several tabs and see message post updates in real time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qrFGj-Vih-Z7OE8omZFjiQ.gif" /><figcaption>Chat Tabs Updating in Real Time</figcaption></figure><p>In fact, every action is automatically sync’ing itself across your server boundary. Even as you add more actions and reducers it will just distribute automatically. So think of the craziest features you can and go build the greatest chat app ever, don’t worry it’ll keep up with you for free!</p><p>If you didn’t follow along you can see what the end product looks like by checking out the basic-distributed branch of the github repo here:</p><p><a href="https://github.com/Ellisande/epic-redux/tree/basic-distributed">Ellisande/epic-redux</a></p><p><strong>A Missing Step</strong></p><p>If you are looking at our finished product you’ve probably noticed something is off. New users don’t see existing messages or already connected users. This is because we don’t follow one of our rules. In order for the states to be exactly equal the stores must get the same actions <em>and start in the same state.</em> I’ll cover that in the next post, and in general, talk about how to deal with state and actions that shouldn’t be distributed.</p><p><strong>But… but…</strong></p><p>Oh no, there are a bunch of issues here that we didn’t cover! If this was a real chat app, shouldn’t we have more than one room? Doesn’t my server need to do some kind of validation? What if I absolutely have to have server-side or front-end specific logic? Why doesn’t the second person see people already in the meeting?</p><p>These questions and more will be answered in the rest of the Abusing Redux series so stay tuned.</p><p>Thanks for taking the time to read this, I hope you found it cool and/or useful!</p><p><strong>Notes:</strong></p><p><strong>Thinking Isomorphically</strong></p><p>The key to making this pattern work is truly viewing the store as an isomorphic structure. It would have been easy to create REQUEST and RESPONSE actions, but who is requesting, and who is responding? Well it depends on whether its the front end store or the backend. We’re so used to adding behavior that is only server-side or only front-end that it just feels natural to want to do it. It will take some time, and lots of refactoring when you realize you’ve done a bad thing, just keep at it and you’ll be able to reap the rewards.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fdaadea13bb5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>