Head to your terminal and cd into your react project (if you are not familiar with the basic terminal commands here is a post that can help). If you haven't created a react-project check out my quick guide here. Now that you are in your project directory run the following command:
npm install react-bootstrap bootstrap
Now that you have successfully installed the dependency on react-bootstrap. We need a way to route the user around. This is where react-router comes in. To install that dependency run the following command:
npm i react-router
Once again if you're unsure if this has worked checkout your package.json file there will be a call to that dependency in there. Now we have got rid of the boring part let's dig into the documentation. The main documentation you want to take a look at is the react-bootstrap docs. This will give you an overall understanding of the dependency and what you can do with it. Now I'm just doing the shortcut part because this isn't a run of the documentation.
The quick guide or what worked for me was simple:
Add this import to your index.js file: import "bootstrap/dist/css/bootstrap.min.css"; and you should be good to go.
The reason I choose react-bootstrap as it seemed to have all the basic components a dev would want. The list from the documentation is the following: Alerts, Accordion, Badge, Breadcrumb, Buttons, Button Group, Cards, Carousel, Close Button, Dropdowns, Figures, Images, List Group, Modal, Navs, NavBar, OffCanvas, Overlays, Pagination, Placeholder, Popovers, Progress, Spinners, Table, Tabs, Tooltips, and Toasts. As you can see the list is quite extensive but it offers exactly what we need out of the box.
If you head over to the Nav bar documentation, the first one was exactly what I was looking for.
It was simple, readable and it had the functionality I needed. The ability to quickly add links to the Nav bar and offer up dropdowns so that my Nav wasn't too messy. Plus the big copy button drew me in so that's exactly what I did. I created a Navbar component knowing that I would be using this and tweaking it in various places, it would be better to have it in one place and be able to change it everywhere if needed, a general Nav bar shall we say.
The import you needed for your Navbar is = import {Navbar, Container, Nav, NavDropdown} from 'react-bootstrap';
So at this point, I had the skeleton of my Navbar component. I had created it and exported it (export default syntax) so it was ready to use in the App. Now what?
How the hell do I call a component that I have now created inside my App. Using what I had learned from the last post in the App.js file. I cleaned it up and got rid of everything I didn't need from the basic App creation. Which is basically everything. So I had a clean App. Localhost:3000 was showing nothing and I was pretty sure I could now add my Navigation component. I just needed to figure out the syntax. After a quick google, it was pretty simple in the end. The syntax was as follows.
<Navigatorbar />
Navigatorbar is the name of my component, and after a simple import Navigatorbar from './components/Navigatorbar is added to the top of the App.js file. My simple Nav bar was up and running on my page. It didn't do anything but it looked good.
After a very simple text edit I now had what I wanted in terms of content. For you just add what your want, play with it a little bit get the setup you want. There are a few various easy styling edits you can do I went with the dark theme as it's smart and simple. For me, a way to get back home, a few links off to other pages, and a drop-down link for you to go off to the different collections you can search for was all I needed.- Navbar you should be comfortable with and you have had a little play. Same with the Nav.link content.
- Link as={Link} from my very basic knowledge of this. Just turns the Nav Link into a react-dom link.
- to={"/url"} sets the URL that is used when the user clicks that Nav item.
- Routes tag wraps your Route tags
- Route tag has path and elements passed.
- path is the URL that that route will check for
- element is what it will load if that path is hit. So element should be the page you want the user to hit if they hit the URL passed. so for my example element={<Pokemom />}
0 Comments