The purpose of this post is to build a new ReactJS app and then integrate React router and React bootstrap
Lets get some basic definitions out of the way. They will be short and sweet I promise :-)
React is a component-based JavaScript library for building user interfaces. (more details here)
Bootstrap is a popular HTML, CSS and JS library for building responsive websites (more details here)
A popular routing library for React (more)
create-react-app is definitely your friend; A very handy utility which lets you create a new React app without requiring you to getting into any configurations. Run the following command to install it
npm install -g create-react-appcreate-react-app my-react-appAnd change to the newly created app directry
cd my-react-appAt this point you should be able to run the app using the following command
npm startAnd see something like this when you visit localhost:3000 in your browser
As stated in the beginning, we want to implement routing using React router and also integrate with React bootstrap
npm install --save react-router-dom
npm install --save react-bootstrap bootstrap@3
This package provides integration between React router and bootstrap. It primarily provides a LinkContainer component which we will use to wrap bootstrap components where ever we need to specify a link
npm install --save react-router-bootstrap
This is not a React router or Bootstrap tutorial so I will not dwelve in the implementation details, but feel free to ask by posting a comment
Copy the following code and save it in a file called MainNavBar.js under my-react-app/src/
import "bootstrap/dist/css/bootstrap.css";
import "bootstrap/dist/css/bootstrap-theme.css";
import React from "react";
import Navbar from "react-bootstrap/lib/Navbar";
import Nav from "react-bootstrap/lib/Nav";
import NavItem from "react-bootstrap/lib/NavItem";
import { Link } from "react-router-dom";
import { LinkContainer } from "react-router-bootstrap";
class MainNavBar extends React.Component {
render() {
return (
<Navbar collapseOnSelect>
{" "}
<Navbar.Header>
{" "}
<Navbar.Brand>
{" "}
<Link to="/"> React-Bootstrap </Link>{" "}
</Navbar.Brand>{" "}
<Navbar.Toggle />{" "}
</Navbar.Header>{" "}
<Navbar.Collapse>
{" "}
<Nav>
{" "}
<LinkContainer to="/">
{" "}
<NavItem eventKey={1}>Home</NavItem>{" "}
</LinkContainer>{" "}
<LinkContainer to="/about">
{" "}
<NavItem eventKey={2}>About</NavItem>{" "}
</LinkContainer>{" "}
</Nav>{" "}
</Navbar.Collapse>{" "}
</Navbar>
);
}
}
export default MainNavBar;
This is where you will include the MainNavBar and also define routes using React router. Open App.js and copy past the following code
import React, { Component } from "react";
import "./App.css";
import { BrowserRouter as Router, Route } from "react-router-dom";
import MainNavBar from "./MainNavBar";
import Grid from "react-bootstrap/lib/Grid";
import Row from "react-bootstrap/lib/Row";
class App extends Component {
render() {
return (
<Router>
{" "}
<Grid fluid={true}>
{" "}
<Row>
{" "}
<MainNavBar />{" "}
</Row>{" "}
<Row>
{" "}
<Route exact path="/" component={Home} />{" "}
<Route path="/about" component={About} />{" "}
</Row>{" "}
</Grid>{" "}
</Router>
);
}
}
const Home = () => (
<div>
{" "}
<h2>Home Page</h2>{" "}
</div>
);
const About = () => (
<div>
{" "}
<h2>About Page</h2>{" "}
</div>
);