<?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 Joseph N. Palomino on Medium]]></title>
        <description><![CDATA[Stories by Joseph N. Palomino on Medium]]></description>
        <link>https://medium.com/@Joe_Pal50?source=rss-b97848c1a087------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fj8PstkhIn6HNMgJljPNWQ.jpeg</url>
            <title>Stories by Joseph N. Palomino on Medium</title>
            <link>https://medium.com/@Joe_Pal50?source=rss-b97848c1a087------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 17:26:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Joe_Pal50/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[How to create a component slider with react-spring]]></title>
            <link>https://medium.com/@Joe_Pal50/how-to-create-a-component-slider-with-react-spring-cc3d8881376b?source=rss-b97848c1a087------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc3d8881376b</guid>
            <category><![CDATA[ux]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Joseph N. Palomino]]></dc:creator>
            <pubDate>Tue, 15 Jan 2019 13:45:33 GMT</pubDate>
            <atom:updated>2019-01-15T13:45:33.857Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yKS-Bi5fDIDo4p-Dbb6tFQ.png" /></figure><p>Recently I had the opportunity to use <a href="https://github.com/react-spring/react-spring">react-spring</a> on the redesign of my portfolio <a href="https://www.josephnpalomino.com/">site</a>. I’ll share why I chose to use the library, how I used it, and what I learned.</p><h4>The why</h4><p>The problem I faced was that instead of the main content scrolling vertically I wanted the user to be able to click or tap(mobile) and have it change screens. Almost like a slider but, unlike most sliders there would be no thumbnails and with the power of react the components that are not currently visible are not actually rendered in the DOM and are only rendered when it’s time for them to appear. This is where react-spring comes in handy. The library has an API to animate the transitions of when a component is mounted and unmounted. I used this API to make it seem like the components were just hidden out of view.</p><p>To download the library go <a href="https://github.com/react-spring/react-spring">here</a> and follow the instructions. I also use emotion to style my components which you can find <a href="https://emotion.sh/">here</a> and code sandbox for this tutorial which you can find the finish product <a href="https://codesandbox.io/s/l2l8j185k7">here</a>:</p><h4>Step 1: Create the container</h4><p>The first thing we want do is create the container component. Since our components will be mounting and un-mounting within we have to set some defaults like height, width, etc:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o8U_ZC84hMM7ST7iik8XSQ.png" /></figure><p>A few things to note here:</p><p>the &amp; &gt; div styles will target a div that is an immediate child instead of all divs that are children and grand children and so on.</p><p>We have to set that div’s position to absolute because, when a component un-mounts and animates off the screen its height will still be visible for a slight second making the new component start at the bottom and then jerk up. This will prevent that.</p><p>Since we set the child div to absolute we then have to set an explicit height for the container. I chose 600px but it should be determined by the height of your content.</p><h4>Step 2: Add your react components</h4><p>For this tutorial I’ll just be adding test screens with the numbers 1, 2, 3 but, you can use any components you want and I demonstrate this by building out each component:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/510/1*vsB-oqcUFtRuHUBPS1Xi4w.png" /></figure><p>A couple of things to note here:</p><p>Emotion allows you to pass in props which you can use to affect your component styles. Pretty awesome.</p><p>We hold our components in an array so that we can animate through them later.</p><h4>Step 3: Set up our state and click event</h4><p>We are going to use component state to know which screen should be rendered. We will do this by creating an index property that will be mapped to our testScreens array. We will also create a click event on the container so that when a user clicks it; it will render the next screen.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*ZJHqO6kJicrAI6pD-9iRIg.png" /></figure><p>A few things to note here:</p><p>We switched our App component from being a stateless functional component to a class component so that way we can use state.</p><p>We set the initial state index to 0 so that way it will render the first screen in the array.</p><p>The toggle function first checks to see if we are at the last index of the array if so we will reset to 0 if not we will just add another to the current sate index.</p><h4>Step 4: Set up our transition element</h4><p>Now we will use the Transition component from react-spring. There are a few things it needs to know:</p><ol><li>The item(s) we will be transitioning (the index from our state will work)</li><li>The animation styles</li><li>The component that will be rendered</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*LR9dyxT6Z90Po-l1L-SZMg.png" /></figure><p>A few things to note here:</p><p>We set the items props to our state.index. We can do this because Transition only uses this to detect changes and since we are calling setState in our toggle method this will update the item passed in.</p><p>The “from”, “enter”, and “leave” props is how Transition will animate the item you pass when it’s going to be mounted, mounts, and when it un-mounts.</p><p>We have to pass the animated styles to the dynamic child that’s why we do style={{...style}}</p><p>The last thing is we use React.createElement to render our component from our component array testScreens by using the index that is passed in.</p><p>That should do it. You can play around with the final product here:</p><p><a href="https://codesandbox.io/embed/l2l8j185k7">react-spring-transition-tutorial - CodeSandbox</a></p><h4>Final thoughts:</h4><p>React-spring is awesome and it allows you to do some pretty common animations with very easy setup. Plus the declarative components help with the overhead of trying to understand whats going on. My only complain is I wish I didn’t have to apply the animation styles to the component that is passed in because it just becomes boilerplate.</p><p>I hope you enjoyed this tutorial, I’m new to writing articles so I’m sure there will be typos and grammatical mistakes. Hopefully these will get better over time as I keep writing and getting feedback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc3d8881376b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>