Exercise: React UI Components and Styling
Objective#
Your objective is to continue from the React Routing Exercise to create an individual page view for each film. Additional goals will be to display film stats based on the current filter applied to our list of films.
Getting Started#
This is part 7 of TrueCoders' React Exercise Series.
You'll be in the same react repository that you worked in for part 6.
Steps#
React Series Part 7 has two parts with multiple exercises each.
- Exercise 1: Install Bootstrap
- Exercise 2: Setup Global CSS and Assets
- Exercise 3: Create a Nav Component
- Exercise 4: Create a Main Layout
- Exercise 5: Create a Loader Component
- Exercise 6: Export Components from an Entry Point File
Exercise 1: Install Bootstrap#
- Use npm to install bootstrap and react-bootstrap
Exercise 2: Setup Global CSS And Assets#
Create a
global.cssfile in yoursrc/folder if you do not have one alreadyPlace the following code within the file
Change the css file import in your
index.jstoglobal.cssCreate a new folder in
src/calledassets/- This is where you can store assets, such as images
Feel free to change any of this css to make it your own. The most important statement is the
@import(...)statement at the top of the file that will load Bootstrap's css class rulesets. I will referencelogo.svgandTCLogoOnly.pngfiles later in this exercise. Thelogo.svgis the react svg that was added to this project when you began in part 1. TheTCLogoOnly.pngwill be used as the navbar brand. You can use any image for that step in the exercise.
Exercise 3: Create a Nav Component#
Create a new folder called
layout/in your existingcomponents/folderCreate a new folder called
topnav/in your newlayout/folderCreate a new file called
topnav.component.jsxin your newtopnav/folderAdd the following imports
Create a functional component called
TopNavand have it return:
I am using a TrueCoders png for the
Navbar.Brandimg. You will either add a new image or use an existing image in yourassets/folder.
Exercise 4: Create a Main Layout#
Create a new folder in
layout/calledmainlayout/Create a new file in
mainlayout/calledmainlayout.component.jsxAdd the following imports
Create a functional component called
MainLayoutDestructure
classNameandchildrenfrom thepropsparameterHave the component return the following:
This layout component will be used as the universal layout for each view of our React application.
Exercise 5: Create a Loader Component#
Create a generic, reusable Loader component.
Create a new folder in
components/calledcommon/Create a new folder in
common/calledloader/Create a new file in
loader/calledloader.css, and add the following contents:Create a new file in
loader/calledloader.component.jsxImport the
loader.cssfileCreate a functional component called
LoaderDestructure
sizefrom the props parameterReturn the following:
Exercise 6: Export Components from an Entry Point File#
Import
MainLayout,TopNav,Loaderinto a newindex.jsfile in yourcomponents/folderExport all components from this entry point file
This entry point file is a common practice. We can now import any of our components from this one file directly inside of the
components/folder.
Add React Bootstrap to the Page Views#
Update the
SingleFilmPagecomponent to return:Update the
FilmsPagecomponent to return:Update the
HomePagecomponent to return:
Helpful Links#
If you feel stuck, or would like to see the finished code for this exercise to check your work, check out: