How to build a simple HackerNews feed with styled-components

areiterer
1,093 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

The App

Now we just have to use the <StoryList> component somewhere in the app, and pass an array of HackerNews stories and it will render a list item for each of them. Since I did not use the API yet, I just used a static array with sample stories which I manually got from the API for the purpose of creating the basic components.

Since the App component is the root of the application, i also used it to define some global CSS rules. Styled-Components provides functionality to inject CSS globally, but they do only recommend to use it for styling the body or defining the font-face globally. (See injectGlobal)

Now let's see what we created, press "Run" and see our finished HackerNews feed:

The finished HackerNews Feed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React from "react";
import PropTypes from "prop-types";
import * as utils from "../../utils/helper";
import {
HeaderWrapper,
RankContainer,
Title,
SourceContainer,
SourceLink
} from "./Styles";
export default function Header(props) {
let url = props.url;
let sourceUrl;
if (!url) {
url = utils.getItemUrl(props.itemId);
}
sourceUrl = utils.getSourceUrl(url);
return (
<HeaderWrapper>
<RankContainer>
{props.rank}.
</RankContainer>
<Title href={url}>
{props.title}
</Title>
<SourceContainer>
(<SourceLink href={"http://" + sourceUrl}>{sourceUrl}</SourceLink>)
</SourceContainer>
</HeaderWrapper>
);
}
Header.propTypes = {
itemId: PropTypes.number.isRequired,
rank: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
url: PropTypes.string
};
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
1
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

As you can see, I retrieve my stories from my static API within componentWillMount and put them into the state. This will change as soon as the app will fetch new Stories from the API, which I will handle in a later blog post.

Now that everything is tied together, the app renders a list of a few HackerNews stories with a similar styling to the original HackerNews list.

What did we learn?

I did not yet fully discover and use every feature of styled-components, but I hope I could show you the basic ideas behind it. I'm amazed of the positive effect of such little things like not having to add class names to components or having to maintain one big or multiple smaller CSS files instead of keeping the style as close to the component as possible.

If you're like me, it might take some time for you until you get used to this (kind of) different way of thinking, but once you get the hang of it, you want to make everything a styled component 🙂

Wrap up

Thank you for reading to the end! This is the first post I have ever written - and also the first one that I posted on tech.io, so I really appreciate every kind of feedback.

This article was originally published at my blog. If you liked it, please have a look at my other posts there.

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content