Reputation: 63
I've been working on a React Bootstrap Navbar for my web app and have had some frustrations with styling it. Essentially, I am trying to set a custom background color for my navbar using custom CSS and classNames, but whether or not the color displays seems to depend on the route the user is visiting.
The colors display as expected when I visit any 'primary' route (eg. /, /about, and /donate appear like this). However, whenever I visit any 'secondary' routes, is seems like the CSS is just flat out ignored (for example /help/contact and /donate/methods appear like this). In all of the above examples, the base URL is localhost:3000. I am using React Router ('react-router-dom') to handle routing and React Bootstrap to create my navbar.
To apply the custom background color, I simply added a className to the Navbar object:
'../components/AppNavBar'
<Navbar variant="light" className=custom-color-1>
*Navbar contents*
</Navbar>
and selected this class in a custom CSS file linked directly to index.html:
.custom-color-1 {
background-color: #B3E5FC;
}
These are the tests I ran that led me to this conclusion:
import React from 'react';
import AppNavBar from '../components/AppNavBar';
const TestPage1 = () => {
return(
<>
<AppNavBar />
Sample text.
</>
);
};
export default TestPage1;
Furthermore, when using Chrome DevTools, the DOM appears to be identical between the two pages. This is the part that baffles me the most. On the pages where the style applies appropriately, I can see my custom className appear in the 'styles' tab of the DevTools panel with the appropriate backgroundColor property. On the other pages, it disappears altogether! The only factor that seems to affect this behavior is the route that each page is mapped to. Is this a bug with React Router, or am I simply overlooking something?
I ran a similar test with text colors on some pages. On the pages where the navbar styles properly, I am able to change the color of any text item (h3, p, etc.) using a className and custom CSS (using the color: #FAFAFA; property). On pages where the navbar does not style properly, I am unable to change text colors. Therefore, it does not appear to be an issue with the React Bootstrap Navbar.
EDIT: Also noticed that I started getting "Resources interpreted as Stylesheet but transferred with MIME type text/html" errors for all of my custom stylesheets. A copy of my index.html file can be found in the sandbox I made, which reproduces my issue.
Upvotes: 2
Views: 2238
Reputation: 62763
There are 2 issues I see here.
More details on the above:
You are using a relative path (./style.css
) to link to your style.css
file. This works on the top-level paths, since the relative path to the style.css
is correct. But on the 2nd-level paths the relative path is no longer correct.
Change the link to the style.css
to
<link rel="stylesheet" href="/style.css" />
You are not using the correct Link component, which compounds the issue above. Currently when a nav link is clicked the page is refreshed with the new route, instead of working as a single-page app.
You'll want to update the <Nav.Link>
components to use the Link
component, like this:
import { Link } from "react-router-dom";
<Nav.Link as={Link} to="/other/1">
Notice the as
and to
props. This will make the <Nav.Link>
work as a <Link/>
compontent and the app will work as a single-page app.
Also, since the <AppNavBar />
is used on every route you should look into moving it out of the individual route components so it's used across all routes.
Upvotes: 2
Reputation: 1985
because you are using react-bootstrap's Nav.Link instead of react-router's {Link} component Nav.Link not a router
first import this in AppNavBar.js
import { Link } from 'react-router-dom';
then change this lines :
<Nav.Link href="/other">Second page using route /other</Nav.Link>
<Nav.Link href="/other/1">Second page using route /other/1</Nav.Link>
with:
<Link to="/other">Second page using route /other</Link>
<Link to="/other/1">Second page using route /other/1</Link>
changes : HERE
Upvotes: 0