Build a realtime Instagram clone — Part 1: Setting up the project and creating the UI
You should have Node and NPM installed on your machine.
This is part 1 of a 4 part tutorial. You can find part 2 here, part 3 here and part 4 here.
Ever used the web version of Instagram? Ever wondered how they make it all work? In this series, we are going to look in depth at how to build an Instagram clone using React.
Get ready because this is going to be a fun ride!
This article is meant for beginners, so feel free to follow through if you’re getting started with React
What is React?
React is an open-source frontend JavaScript framework used for building incredible, reusable user interfaces. Find out more about React, you can head over here.
Prerequisites
Before we can set up a React project, we need to have the following :
To confirm your Node/NPM installation, run the following command on your terminal :
node --version
npm --version
If you get version numbers as results, then you’re good to go. If not, head over to the Node installation page and/or NPM installation page to get them set up.
Setting up a React project
Installing React
React is available as a node package and to get React installed on your machine, you need to run the command :
npm install -g create-react-app
💡 You need to have Node version >= 6 to install React
This globally installs the package for you to use as you please.
Creating our React application
To create our application, we use the create-react-app
we installed by running the the command :
create-react-app instagram-clone
This helps set up the development environment that gets you started with creating React applications.
Taking our application for a spin
Now to confirm and test the creation of our new application, we change directory to our instagram-clone
directory and then start our development server by running :
cd instagram-clone
npm start
Your development server should start and you should get a page that looks like this:
Creating UI components
Now that we are ready to start developing react applications, the next thing we are going to do is design the components we are going to need in our application.
What are components
If you’re new to React, you may be wondering what components are. Wonder no more. Components in React give you the ability to build your UI in bite-sized bits. Instead of you building the whole interface in a single file, you break it down to independent and reusable pieces which you then put together to have your application as a whole.
Deciding what components we need
Now, let’s think about the components needed. For starters, we can break down the components into two:
- Header component
- Post component
The Header component will contain the instagram logo and brand name while the Post component will contain the contain image and caption a user has posted.
Header component
Now we create a components
folder in the src/
directory of our application.
cd src
mkdir components && cd components
We then create a folder for our Header component:
mkdir Header && cd Header
Now that we have our Header component, the next thing we want to do is to create our index.js
file in the Header directory:
touch index.js
Open index.js
and past in the following:
// src/components/Header/index.js
import React from "react";
class Header extends React.Component{
render(){
return (
<nav className="Nav">
<div className="Nav-menus">
<div className="Nav-brand">
<a className="Nav-brand-logo" href="/">
Instagram
</a>
</div>
</div>
</nav>
);
}
}
export default Header;
In React we describe our component with JSX. JSX looks similar to pure HTML but there are some differences between them.
💡 Notice how the class names are being added in JSX and compare it to regular HTML
Styling our Header component
Now, we are going to add the necessary style that makes our Header component look pleasing to the eye. To do this, we create a Header.css
file in our src/components/Header
directory. Open the Header.css
and paste the following:
/* src/components/Header/Header.css */
i.Nav {
background-color: #fff;
border-bottom: 1px solid rgba(0, 0, 0, 0.0975);
position: fixed;
top: 0;
width: 100%;
z-index: 2;
-webkit-transition: height 0.2s ease-in-out;
transition: height 0.2s ease-in-out;
height: 77px;
}
.Nav-menus {
display: flex;
flex-direction: row;
height: 77px;
width: 70%;
margin: 0 auto;
padding: 26px 40px;
}
.Nav-brand-logo {
display: block;
background-position: -176px 0px;
background-image: url(../../sprite.png);
background-size: 405px 379px;
background-repeat: no-repeat;
height: 35px;
width: 176px;
text-indent: -1000%
}
💡 You need to add the
[sprite.png](https://github.com/christiannwamba/instagram-clone/blob/master/src/sprite.png)
in thesrc
directory of the application. Download it here
Linking the style in our component
We head back to our Header component and add the following :
// src/components/Header/index.js
import "./Header.css";
class Header extends React.Component{
// ....
}
export default Header;
Once we link the style sheet as we did above, we are good to go.
Rendering the Header component
Now that we have successfully built our Header component, the next thing we want to do is to render it. To that, we need to tweak our src/App.js
file.
// src.App.js
import React, { Component } from 'react';
import './App.css';
import Header from './components/Header';
class App extends Component {
render() {
return (
<Header />
);
}
}
export default App;
Once we do this, we have our Header component added and the app looks like this:
Post component
To create a Post component, we create a folder called Post
in the src/components
directory,
cd src/components
mkdir Post && cd POst
We then create the index.js
file. Open it and paste in the following:
// src/components/Post/index.js
import React, { Component } from "react";
class Post extends Component {
render() {
return <article className="Post" ref="Post">
<header>
<div className="Post-user">
<div className="Post-user-avatar">
<img src="https://www.laravelnigeria.com/img/chris.jpg" alt="Chris" />
</div>
<div className="Post-user-nickname">
<span>Chris</span>
</div>
</div>
</header>
<div className="Post-image">
<div className="Post-image-bg">
<img alt="Icon Living" src="https://pbs.twimg.com/media/DOXI0IEXkAAkokm.jpg" />
</div>
</div>
<div className="Post-caption">
<strong>Chris</strong> Moving the community!
</div>
</article>;
}
}
export default Post;
Here we see the structure of the posts outlined. We have the:
- Post Header - shows the users avatar and name
- Post Content - displays the post content
- Post Caption - displays the username and post caption
Styling our Post component
We create a Post.css
file in the src/components/Post
directory. Open Post.css and paste in the following:
/* src/components/Post/Post.css */
.Post {
border-radius: 3px;
border: 1px solid #e6e6e6;
background-color: #fff;
margin-bottom: 60px;
margin-left : 20%;
margin-right: 20%;
}
.Post-user {
display: flex;
padding: 16px;
align-items: center;
}
.Post-user-avatar {
width: 30px;
height: 30px;
}
.Post-user-avatar img {
width: 100%;
height: 100%;
border-radius: 50%;
}
.Post-user-nickname {
margin-left: 12px;
font-family: 'PT Sans', sans-serif;
font-weight: bold;
}
.Post-image-bg {
background-color: #efefef;
}
.Post-image img {
display: block;
width: 100%;
}
.Post-caption {
padding: 16px 16px;
}
.Post-caption strong {
font-family: 'PT Sans', sans-serif;
font-weight: bold;
}
.vjs-fade-out {
display: none;
visibility: hidden;
opacity: 0;
}
Linking the style in our component
We head back to our Post component and add the following :
// src/components/Post/index.js
import "./Post.css";
class Post extends React.Component{
// ....
}
export default Post;
Rendering the Post component
Now we go ahead to render the Post component itself. We edit our App.js
file to make it look like this :
// src/App.js
import Post from './components/Post';
class App extends Component {
render() {
return (
<div>
<Header />
<div>
<Post />
</div>
</div>
);
}
}
export default App;
Now, when we go back to our page, we have this :
Rendering components with mock data
You already noticed that in our Post component had a lot of static data - every time you reload you only see the same post. In a real-life application, what we want is to have our list of dynamic posts when our application is accessed. To do this, we are going to tweak our Post component.
Using props in our Post component
In React, props
as the name suggests, are the properties of a particular component. They help in making sure that our components are reusable. Update your Post component to look like this :
// src/components/Post/index.js
import React, { Component } from "react";
import "./Post.css";
class Post extends Component {
constructor(props){
super(props);
}
render() {
const nickname = this.props.nickname;
const avatar = this.props.avatar;
const image = this.props.image;
const caption = this.props.caption;
return (
<article className="Post" ref="Post">
...
<img src={avatar} alt={nickname} />
...
<span>{nickname}</span>
...
<img alt={caption} src={image} />
...
<strong>{nickname}</strong>{caption}
...
</article>
);
}
}
export default Post;
We accept the props from when the Post itself is being rendered and then display the results to the users in form of posts.
Now, the src/App,js
is also tweaked to pass the data to the component like this:
// src/App.js
import React, { Component } from 'react';
import './App.css';
import Header from './components/Header';
import Post from './components/Post';
class App extends Component {
render() {
return <div className="App">
<Header />
<section className="App-main">
<Post nickname="Chris" avatar="https://www.laravelnigeria.com/img/chris.jpg" caption="Moving the community!" image="https://pbs.twimg.com/media/DOXI0IEXkAAkokm.jpg" />
<Post nickname="OG" avatar="https://www.laravelnigeria.com/img/chris.jpg" caption="Holding a mic" image="https://pbs.twimg.com/media/DOXI0IEXkAAkokm.jpg" />
{/* more posts */}
</section>
</div>;
}
}
export default App;
Now, when you visit the application at localhost:3000
, you get a page that looks like this:
Conclusion
In this chapter of the series, we looked at how to get set up with React development and creating the UI for an Instagram clone application. In the next chapter, we will take a look at how to connect the UI to Graph QL data. Here’s a link to the full Github repository.
27 April 2018
by Christian Nwamba