JavaScript Links from navbar made with React don't work

  • Thread starter Thread starter Darkmisc
  • Start date Start date
  • Tags Tags
    Router
AI Thread Summary
The navbar created from a YouTube tutorial is not functioning correctly, as the pages do not load despite the URL updating upon clicking the links. The user has made necessary updates to the code due to deprecated methods but still encounters a blank page under the navbar. They suspect the styling and imports are correct since there are no compiler errors when page names are misspelled. A suggestion was made to consult the official documentation for the current version of React and React Router, as the original tutorial is outdated. The user is encouraged to start with a basic, functional setup before adding complexity.
Darkmisc
Messages
222
Reaction score
31
TL;DR Summary
I downloaded code for a navbar with dropdown menus made with React. Each link is supposed to load a page, but the pages won't load.
Hi everyone

I downloaded a navbar from a youtube tutorial from https://github.com/briancodex/react-navbar-dropdown

The video is three years old, so I had to change "switch" in the code to "routes" in App.js. I also had to change the "render" method in index.js because it had been deprecated. The navbar seems to work, except the page is blank under the navbar.

1714947522905.png


There are supposed to be backgrounds for every link in the navbar. This is how it looks in the video.
1714947574517.png


The URL updates when I click on the links, but the pages won't load.
1714947654420.png

I think the code for the styling and imports are correct. I found this out by accident while playing around with the code for the dropdown menu. I renamed the className for the dropdown menu to "home" and the styling for home got applied to the dropdown menu. That's why in the below image you see part of the home page instead of the dropdown menu.

1714947975820.png


There is a js file for each page that is supposed to load. I think they've been imported/exported correctly (the compiler gives me an error message when I deliberately misspell the page names, otherwise I get no error message).

Does anyone know why the pages won't load?

Below is the code for App.js, App.css, index.js and one of the pages.

I can upload the rest of the code if necessary. It's also available from the github link. I haven't made any changes to the rest of the code.

Thanks

[CODE lang="jsx" title="App.js"]import React from 'react';
import Navbar from './components/Navbar';
import './App.css';
import Home from './components/pages/Home';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Services from './components/pages/Services';
import Products from './components/pages/Products';
import ContactUs from './components/pages/ContactUs';
import SignUp from './components/pages/SignUp';
import Marketing from './components/pages/Marketing';
import Consulting from './components/pages/Consulting';
import { createRoot } from 'react-dom/client';



function App() {
return (

<Router>
<Navbar />
<Routes>
<Route path='/' exact component={Home} />
<Route path='/services' component={Services} />
<Route path='/products' component={Products} />
<Route path='/contact-us' component={ContactUs} />
<Route path='/sign-up' component={SignUp} />
<Route path='/marketing' component={Marketing} />
<Route path='/consulting' component={Consulting} />
</Routes>
</Router>
);
}

export default App;
[/CODE]


[CODE lang="jsx" title="Marketing.js"]import React from 'react';


export default function Marketing() {
return (
<>
<h1 className='marketing'>MARKETING</h1>
</>
);
}
[/CODE]

[CODE lang="jsx" title="index.js"]import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';


// ReactDOM.render(<App />, document.getElementById('root'));

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
[/CODE]

[CODE lang="css" title="App.css"]* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'PT Sans', sans-serif;
}

.home,
.services,
.products,
.contact-us,
.sign-up,
.marketing,
.consulting {
display: flex;
height: 90vh;
align-items: center;
justify-content: center;
font-size: 3rem;
}

.home {
background-image: url('./images/img-1.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.services {
background-image: url('./images/img-2.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.products {
background-image: url('./images/img-3.jpg');
background-position: center;
background-size: fill;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.contact-us {
background-image: url('./images/img-4.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.sign-up {
background-image: url('./images/img-7.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.marketing {
background-image: url('./images/img-6.jpg');
background-position: center;
background-size: cover;
background-repeat: no-repeat;
color: #fff;
font-size: 100px;
}

.consulting {
background-image: url('./images/img-5.jpg');
background-position: bottom;
background-size: cover;
background-repeat: no-repeat;
/* color: #fff; */
color: red;
font-size: 100px;
}

/*testing below*/
.btn {
background-image: url('./images/img-3.jpg');
color:red;
}[/CODE]
 
Technology news on Phys.org
Consult the official documentation for the version of React (and in particular the version of React Router) you are using, not a three year old video.

Hint: it is always easier to start with something that looks basic but works then make it look pretty than to start with something that looks pretty but doesn't work.
 
It works now with
[CODE lang="jsx" title="App.js"]

import {Routes, Route} from 'react-router-dom';


function App() {
return (
<div className='router'>
<Navbar />
<Routes>
<Route path='/' element={<Home/>} />
<Route path='/services' element={<Services/>} />
<Route path='/products' element={<Products/>} />
<Route path='/contact-us' element={<ContactUs/>} />
<Route path='/sign-up' element={<SignUp/>} />
<Route path='/marketing' element={<Marketing/>} />
<Route path='/consulting' element={<Consulting/>} />
</Routes>
</div>
);
}[/CODE]
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
5
Views
2K
Replies
5
Views
2K
Replies
8
Views
2K
Replies
3
Views
3K
Replies
2
Views
9K
Back
Top