1.1: Project Setup
Setting up our folders
Now that we have a working React project, let's talk a little bit about components. The idea behind components is the separation of concerns of parts of our app. So if you think about Instagram, we might have one component for the picture, one for likes, one for comments, one for editing, etc. We use components to make development more organized, and for our projects to be more easily changed in the future.
For our project, we're going to make a folder inside of src called components. This is where we will house our components for our app!
We're going to organize our project further, and make folders for specific parts of our project, to make it more organized and easier to work with. Inside of your components folder, make a folder called site. This folder will house our components for our site setup!
Making Components
Inside of our site folder we're going to make 5 components. In React, it is necessary to name your React component with starting with an uppercase letter. So ensure that you follow these names exactly. Make components called: Footer.js, Header.js, Home.js, Resources.js and Sidebar.js. Ensure that they are spelled exactly like above.
Footer.js
Make your Footer.js have the following code:
import React from "react";
import { Row } from "reactstrap";
const Footer = () => {
return (
<footer>
<Row>
<p>© Eleven Fifty 2018</p>
</Row>
</footer>
);
};
export default Footer;This is a very basic example of React functional component. We'll break down more of React later, for now, just know that we're using some bootstrap in React form (reactstrap), and that all this component is doing is displaying an Eleven Fifty copyright.
Header.js
Next, in our Header.js add the following code:
Again, we'll go more in depth later, but basically this is just setting up a Bootstrap navbar for us to add on to later!
Home.js
Add the following code to Home.js:
This component has some info about the start of the app for your benefit! Feel free to change this component as we go forwards. There are also these Links that you may see on the page. These are imported from the react-router-dom package which we'll look at soon. This package allows us to navigate within our application.
Resources.js
Add the following code to Resources.js:
This is again just a functional component to display some resources for your use!
Sidebar.js
Add the following code to Sidebar.js:
Now, this is where react-router-dom is even more used. This component is how we set up where we want to go in our application and what we want to see. Our Routes determine what will be shown on the screen at that path, and our Links actually take us to that path! So above if we use a Link to "/" or "/home" then we should see the <Home /> component. Same thing with <Resources /> and "/resources"! This is the whole way we can navigate to places in React! Our Switch acts just like a switch statement in vanilla. Only one of the conditions are true at one time, that way only one part of our application shows at a time!
Back to App.js
Now, you may notice that nothign has changed on our screen yet, because we haven't actually hooked anything up to our MAIN component that we edit in React, App.js! Let's change that!
Make your App.js look like the following:
Another note about react-router-dom. We talked about Link and Route already, but to actually make it work, we have to wrap all those Links and Routes in one BrowserRouter, which we've imported as Router here. This is critical to making this work!
Styling
There are many ways to style React applications, we recommend you research SCSS, CSS in JS, and advanced CSS in the future. For now, we're just going to stick with some simple CSS. Make your App.css look like this:
Note that usually for big applications, you want to split up your CSS into files, or use SCSS.
Now, we're all set to start developing and learning React!!!
Last updated