<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ nba - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ nba - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 25 Jun 2026 04:45:06 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/nba/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How I built an NBA player profile fetcher with React, Redux-Saga, and Styled Components ]]>
                </title>
                <description>
                    <![CDATA[ By Jonathan Puc Hello, all! It’s been a while since I built something out of personal enjoyment or curiosity, so I surfed the internet exploring cool API’s. Since it’s NBA Playoff time (sadly, I’m a Knicks fan), I decided to see if there was an exist... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-a-nba-player-profile-fetcher-with-react-redux-saga-and-styled-components-680cde2b8254/</link>
                <guid isPermaLink="false">66c346628ced2460cf9e9b67</guid>
                
                    <category>
                        <![CDATA[ nba ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sun, 13 May 2018 09:29:12 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*799DwILNz4o4I_PrUqVjvw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Jonathan Puc</p>
<p>Hello, all! It’s been a while since I built something out of personal enjoyment or curiosity, so I surfed the internet exploring cool API’s.</p>
<p>Since it’s NBA Playoff time (sadly, I’m a Knicks fan), I decided to see if there was an existing API that contained the data of every player currently in the NBA — and heck yeah, there was.</p>
<p>Also, a project I’m working on at my job has introduced me to two <strong>awesome</strong> libraries called <strong>redux-saga</strong> and <strong>styled components</strong>. They are pretty darn exciting, and are two things I definitely plan to try and use in all my future projects.</p>
<p>So let’s build a React application with these libraries!</p>
<p>Before we dive in, let’s just talk a bit about redux-saga and styled components and why they are handy.</p>
<h3 id="heading-redux-saga">Redux-Saga</h3>
<p>In Redux, actions and reducers are pure, meaning they don’t have any side effects.</p>
<p>An example of a side-effect could be something like a service request. When you are making a request, it can fail or return a different kind of result even though you always send the same request.</p>
<p>So if your reducers and actions are pure, where can you handle / put side effects? Well redux-saga is a solution. It allows you to listen to actions, perform a side effect, and then dispatch another action.</p>
<p>I know, talk is cheap. Show me the code.</p>
<p>Are you ready to see an example of this beast at work?</p>
<p>In a nutshell, we have a function that listens for whenever an action of type <code>‘IMAGE_FETCH_REQUESTED’</code> is dispatched. When it identifies one, it’ll call the fetchImage function.</p>
<p>Inside the fetchImage function, we simply make a special <code>call</code> to a method on our <code>service</code> object, passing along the <code>userId</code> of the profile image we want to grab. The result gets saved inside our profileImage variable.</p>
<p>Shortly after, we let our store know that we have successfully grabbed an image and would like to pass the image on to be stored. So we’ll just dispatch an action with <code>put</code> with the type of <code>'IMAGE_FETCH_SUCCEEDED'</code> and pass the image as payload. Our reducer will handle the rest.</p>
<p>But <strong>if</strong> there is some kind of error, we simply dispatch an action with the type<code>'IMAGE_FETCH_FAIL'</code> and pass the error as payload.</p>
<p>The beauty of it lies in how nicely it reads and sits within a simple try catch block.</p>
<p>Feel free to read more about <a target="_blank" href="https://github.com/redux-saga/redux-saga">redux-saga</a>.</p>
<h3 id="heading-styled-components">Styled Components</h3>
<p>Discovering styled components kind of blew my mind.</p>
<p>I always had trouble structuring and writing CSS inside React apps. Something didn’t sit right and it felt messy to me. In particular, class names were tough.</p>
<p>The whole idea of React is about being modular: you write a component once and are able to use it everywhere. But when styling such components, we still give them a <strong>class</strong> name to target them with CSS.</p>
<p><a target="_blank" href="https://twitter.com/mxstbr?lang=en">Max Stoiber</a>, co-creator of styled components, put it perfectly when he said:</p>
<blockquote>
<p>If you only ever use every class name once, why do you have a class name at all?</p>
</blockquote>
<p>Having heard those words, I knew styled components was for me.</p>
<p>So let’s see this one at work now too:</p>
<p>Here we have a basic functional component: a button that pretty much does nothing, even though it’s daring you to make your move.</p>
<p>This may look weird to newcomers, but really it’s quite simple and I’m sure you’ll fall in love with it in no time.</p>
<p>We import <code>styled</code> from the library. Think of this as a factory that allows you to create the HTML nodes you all know and love.</p>
<p>We create the node of our liking. In this case, a button and span, with its styles. We then assign it to a variable of our choice.</p>
<p>Now we refer to those variables and pop them within our functional component to be rendered.</p>
<p>It’s as easy as that.</p>
<p>What I really like is that you can still write the CSS you are familiar with in a JS file. Furthermore, it keeps everything nice and modular — everything sits within a single file, easy to read and digest!</p>
<p>You can learn more about styled-components <a target="_blank" href="https://github.com/styled-components/styled-components">here</a>.</p>
<h3 id="heading-how-this-all-links-together">How this all links together</h3>
<p>We’ll be building an application where users can search for a player using their first and last name. Our saga (redux-saga) will fetch the data of the player, including statistics and a headshot of them, and save it into our redux store. And using styled components, we’ll make all this information look a little more presentable.</p>
<h3 id="heading-part-1-setting-up-our-app-and-react-redux"><strong>Part 1 — Setting up our app and react-redux.</strong></h3>
<p>We’ll be using create-react-app in this project, so if you haven’t yet got it installed, just run <code>npm install -g create-react-app</code> .</p>
<p>When that’s done, we’ll run <code>create-react-app nba-players</code> .</p>
<p>Now after all the installing and scaffolding is done, we’ll <code>cd nba-players</code> and then install the modules we’ll need with <code>npm install --save redux react-redux redux-saga styled-components axios</code> .</p>
<h4 id="heading-setting-up-our-redux-store">Setting up our redux store</h4>
<p>This will be a quick walkthrough of getting our store set up, since this guide is about redux-saga and styled components and not about react-redux/redux.</p>
<p>Inside your <code>src</code> folder, we’ll create a folder called <code>store</code> and create our <code>index.js</code> file.</p>
<p>store/index.js</p>
<p>We’ll be using Redux DevTools to see what’s going on under the hood in our store. You can download the Chrome extension <a target="_blank" href="https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd?hl=en">here</a>.</p>
<h4 id="heading-lets-create-our-reducers"><strong>Let’s create our reducers.</strong></h4>
<p>Make a folder called <code>reducers</code> within the root of your <code>store</code> folder, and create the two following files:</p>
<p>reducers/index.js</p>
<p>reducers/player.js</p>
<h4 id="heading-lets-create-our-actions"><strong>Lets create our actions</strong></h4>
<p>Make a folder called <code>actions</code> within the root of your <code>store</code> folder, and create the two following files:</p>
<p>actions/actionTypes.js</p>
<p>actions/player.js</p>
<p>With all those pieces created, let’s connect the store to our React application!</p>
<p>Navigate your way to <code>src/index.js</code> and add the following:</p>
<p>Sweet, let’s test and make sure everything is working as expected.</p>
<p>Back in our terminal, we’ll run <code>npm run start</code> to fire up our React app, open the developer tools, and navigate to the ‘Redux’ tab. Click on the State tab within the Redux DevTools.</p>
<p>You should see something like this :)</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*klP5Nqu0Bwibd-KI9SimWw.png" alt="Image" width="800" height="288" loading="lazy"></p>
<p>Awesome, we’ve got everything we need to get started.</p>
<h3 id="heading-part-2-redux-saga">Part 2— Redux Saga</h3>
<p>We’re ready to utilise the NBA player API to fetch data and load it into our store!</p>
<p>Let’s write our first saga.</p>
<p>Inside our <code>src/store</code> folder, we’ll create a folder called <code>sagas</code> and create a file called <code>index.js</code> .</p>
<p>This basically serves as our watcher / gatekeeper.</p>
<p><code>Line 8</code> sits there and listens for certain action types we give it. When an action passes through that matches, it’ll call a function, in this case retrievePlayer. We’ll create that now.</p>
<p>Within the same folder, we’ll create a file called <code>player.js</code> and it’ll contain the following:</p>
<p>The retrievePlayer generator function is where the magic happens, so let’s walk through it.</p>
<p>The function has access to the action that’s passed through. If you can recall from our action creator in <code>actions/player.js</code> , we pass a name.</p>
<p>We’ll use ES6 destructuring to get the name and surname from the name object attached to the action payload.</p>
<p>Using redux-saga, we <code>call</code> our fetchPlayerData function and pass in the name details.</p>
<p>fetchPlayerData will make a GET call to the NBA players API and return the response. The response will be saved inside the stats variable.</p>
<p>Access to the players image is as easy as appending the name and surname to the API endpoint, so we do just that.</p>
<p>We save our two new pieces of data into an object called playerProfile.</p>
<p>We then use redux-saga’s put which will dispatch an action. Here we give it the type of <code>GET_PLAYER_SUCCESS</code> with the our new playerProfile as the payload.</p>
<p>If something goes wrong, we simply dispatch an action with the type <code>GET_PLAYER_FAIL</code> and pass the error as the payload.</p>
<p>That’s it!</p>
<p>Our players reducer that we made previously at <code>reducers/player.js</code> will handle the rest after receiving the actions we dispatched.</p>
<p>There is one last thing we need to do before our sagas work, however.</p>
<p>Inside <code>store/index.js</code> we’ll have to make some modifications.</p>
<p>It should now look like the following</p>
<p>Woohoo, we’re now ready to build some components that’ll allow us to search for a player and see their image and stats :)</p>
<h3 id="heading-part-3-styled-components">Part 3 — Styled Components</h3>
<p><code>components/Search.js</code></p>
<p><code>components/StatBox.js</code></p>
<p><code>components/PlayerPhoto.js</code></p>
<p><code>components/Player.js</code></p>
<p>With all our components built, it’s time to import them into our <code>App.js</code></p>
<p>Everything’s hooked up and ready to go. Simply type in the full name of a player to your liking, such as Lebron James or Stephen Curry, and you should see something like this.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*oVmg6dXWrMrl87VnXUxwog.png" alt="Image" width="515" height="805" loading="lazy"></p>
<p>Not the prettiest thing to look at, but this is an opportunity for you to apply styling as you see fit. Go crazy with the styled-components library.</p>
<p>Also remember that we added a <code>loading</code> property in our redux store <code>state.player.loading</code> ? Why not make the UX a little bit nicer by showing a loading message of some kind when loading is set to true?</p>
<p>We’ve created the foundation of the application together — now go on and give it your own personal touch :)</p>
<p>If needed, you can find the source code <a target="_blank" href="https://github.com/jonathanpuc/nba-mania">here</a>.</p>
<p>As always, my inbox is open to anybody in need of further advice or if you have questions.</p>
<p>Feel free to connect with me on any of the platforms below!</p>
<p><a target="_blank" href="https://www.instagram.com/jonathanpucc/">Instagram</a> | <a target="_blank" href="https://www.linkedin.com/in/jonathan-puc-549531b3/">LinkedIn</a> | <a target="_blank" href="https://twitter.com/jonathanpuc7">Twitter</a></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
