Build A MERN Stack App in 5 Days (Day 2: Getting started with the Frontend)
Tags: react, tailwindcss, webdev, javascript
Published: 2022-03-27 07:59:34

Hello and welcome to part 2!
In the previous part, we setup our backend server, created our models and routes and connected to our MongoDB database. In today's part, we will:
-  Setup React + Tailwind CSS
-  Setup the folder structure for our frontend
-  Create the Header, Sidebar & Widgets components  

_Prerequisites_:
- CSS Flexbox
- TailwindCSS (Basic)
- React (Basic) 

This is what the 'Home' page on our final version of the app looks like:

![desk](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ykehlbq30no34az8q815.png)

![mobile](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4xrlxfa755x46kfd7cy.png)

It is mobile responsive and we can divide the page into 4 sections:
- Header
- Sidebar
- Feed
- Widgets

<h3> Setup </h3>
To setup the frontend project, navigate to the 'client' folder and follow this short & concise [doc](https://tailwindcss.com/docs/guides/create-react-app) to setup the boilerplate for a basic React App with Tailwind CSS . This will take about 1-2 minutes.

Once done, navigate to the 'src' folder and go ahead and delete the files we won't be needing, only keep the following files in the 'src':
- App.js
- index.js
- index.css

Make sure to delete the code in all three files and we shall start from scratch! This is what these 3 files should look like:
- App.js

![app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sm8qvmziakw34ls00ff.png)

- index.js

![index](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n8dy8xin6kiq8dhf16ci.png)

- index.css

![css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pc2i2ej2mqoy1zi6nfbg.png)

Now, go ahead and create two folders in the 'src' directory, namely 'components' and 'pages'. In the components folder, create 4 new files, namely 'Header.jsx', 'Sidebar.jsx', 'Widgets.jsx' and 'Feed.jsx' and for the time-being, initialize them as such:

![feed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d6vj3h5434wrfry8uu0d.png) 

Do the same for Sidebar, Header and Widgets components and import them in the new 'Home.jsx' file in the 'pages' directory:

![home](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l39gu0p40c8tvprtr0f1.png)

Go ahead and export the 'Home' component in the App.js in 'src' and render them as such:

![apps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ljrq0ix9kadxnimt42ii.png)

Run 'npm start' in the terminal once you are in the 'client' directory and this is what our site looks like for now:

![demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fyffzm1fadp49ur15zuo.png)

Let's apply some Tailwind utility classes to our 'Home' to align the items properly and setup our layout as follows:

![tc](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/goz9p25hzgciwd9oxft6.png)

![lay](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lohdy4jx8ebd8d40m9a7.png)

Now, let's go ahead and style the individual components one by one. We will go over the Header component in detail, applying Tailwind utility classes and discuss adding custom Tailwind classes. We will discuss the Sidebar & Widgets components in brief. Later, you can go ahead and apply your own styles and make the application your own. Let's get started with the Header!

<h2> Header </h2>

Before we start coding the header, we need to install two packages: @material-ui/core and @material-ui/icons which we will use for the icons and components like Button, Input, Avatar in our application. Alternatively, you can use '@headless-ui/react' and '@heroicons/react' for this purpose.

Our Header will be divided into 3 sections:
- Left (Our logo)
- Center (Icons & Searchbar)
- Right (Avatar & Add Post button)

![headset](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sth65odae08wto31plyp.png)

![sethead](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bd3j68bv875i8aph7tjl.png)

We need to apply 'flex' to our Header to align the items in a row and in the meantime let's apply some styling to our logo, feel free to replace this with an image of your app logo.

![span](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tgl67eo2k4yosid25arq.png)

![flex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mca929lb95rikxx6sg4.png)

Next, we are gonna create our first custom TailwindCSS class to style our header icons, head over to index.css and add the following:

![custom](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kp9085ppa29hr1a36i4u.png)

![add](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7aq938wooz4j90u8pby7.png)

![search](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rqfjy6om586p8jeerw3.png)

Let's add the Avatar and 'Add Question' button and style it up to finish our Header design.

![style](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pqbo7zp6z5azv662nn8.png)

![file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/89nwg6rgxsax1f35xokb.png)

In the same way, let's style our Sidebar and Widgets!

<h2> Sidebar </h2>
In the 'components' folder, create a SidebarItems.jsx file and import it into the Sidebar.jsx fil and style it as such with some more custom TailwindCSS classes!

![sideclass](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3z9wzzc10giczgn159p.png)

![content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fobcfp73el17ln5tlt45.png)

![sidebars](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mk6ttqkysyscf9wygt8y.png)

![shot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owqnz06qwml6b5c3dgtv.png)

<h2> Widgets </h2>
Similarly, for the Widgets component, let's create a WidgetContent.jsx file and style it with custom TailwindCSS classes and import it in the Widgets.jsx!


![widstyle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wlop2m1c4b9ngb7drs7b.png)

![widcomp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/esvxt70e5o3vbdb1fhxl.png)


![contain](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2uct2yrwhlgwdbefflfa.png)

![view](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65za4o7s6zhwywbp7me7.png)

<h3> Final Touches </h3>
As of now, the Feed seems to be of around the same width as Sidebar and Widgets components. However, we want the Sidebar and Widgets to each take up 20% of the screen on large screens and the Feed to take up 60% of the screen. In smaller screens, we want the Sidebar and Widgets to be hidden and the Feed to take up the entire space. Keeping that in mind, we apply the following styles:

![feeder](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a0c12othex02z5hknqdu.png)

![final](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/du85i04qu8ijy8lb70o6.png)

In the next part of the series, we will implement Firebase Google Auth and Login and then make requests to our backend to Add and Fetch posts from our database and create the Feed!

<h3> Homework(Suggestions to practice) </h3>
- Replace the logo in the 'Header' with your own custom logo.

- Replace the multiple divs in the SidebarItems component by mapping over an array to make the code cleaner. (Plus points if you fetch this data from some API)

- Do the same for the Widgets Component!

- Use headlessui + heroicons instead of Material UI

<h4> Resources </h4>
[TailwindCSS](https://tailwindcss.com/)
[Icons](https://mui.com/components/material-icons/)
[Code Repository](https://github.com/kishibe-rohan/kysymys-tutorial)

See you in part 3!