Links for navigation are only functional within the App Component

I have 4 components: App.js, navbar.js, signIn.js, signUp.js. The goal is to have the navbar display at the top of every page the user visits. Currently, the links in the navbar only work on the homepage and are disabled on other pages.

I attempted to resolve this issue by installing react-bootstrap via npm instead of using cdn, but it did not solve the problem. Additionally, I tried creating a new set of components named "NavigationLinks.js", however, that approach also failed. I am unsure of what else to try.

Below are the relevant files:

App.js

import logo from './logo.svg';
import './App.css';
import Navbar from './components/layout/navbar'
import Dashboard from './components/feed/dashboard'
import SignIn from './components/auth/signIn'
import SignUp from './components/auth/signUp'
import { BrowserRouter, Switch, Route } from 'react-router-dom';

function App() {
  return (
      <BrowserRouter>
          <div className="App">
              <Navbar/>
              <Switch>
                  <Route exact path="/" component={Dashboard}/>
                  <Route path="/signin" component={SignIn}/>
                  <Route path="/signup" component={SignUp}/>
              </Switch>
          </div>
      </BrowserRouter>

  );
}

export default App;

Navbar.js

import { Switch, Route, Link} from 'react-router-dom';
import { Navbar, Nav, Form, FormControl, Button, NavItem } from 'react-bootstrap';
//import './navbar.css';

class navbar extends Component {
    render() {
        return (
            <div>
              <div>
                  <Navbar>
                      <Navbar.Brand as={Link} to='/'>Rekindle</Navbar.Brand>
                      <Navbar.Collapse>
                          <Nav className="mr-auto">
                              <NavItem eventkey={1} href="/">
                                  <Nav.Link as={Link} to="/" >Home</Nav.Link>
                              </NavItem>
                              <NavItem eventkey={1} href="/signup">
                                  <Nav.Link as={Link} to="/signup" >Sign Up</Nav.Link>
                              </NavItem>
                              <NavItem eventkey={1} href="/signin">
                                  <Nav.Link as={Link} to="/signin" >Login</Nav.Link>
                              </NavItem>
                          </Nav>
                      </Navbar.Collapse>
                  </Navbar>
              </div>

              {/*this div works for the routing  */}
              <div>

              </div>
            </div>
        );
    }
}


export default navbar;

The code compiles successfully, but the issue remains with navigating back to the homepage from the signIn and signUp routes.

Answer №1

I noticed a few issues in your code snippet, specifically that you forgot to include the import statement for react.

Remember, whenever you create a new file, you must import React like so:

import React from 'react'; //default

Additionally, when extending the Component in your navbar component, make sure to also import it from the react package.

There are 2 ways to extend Component:

Way 1:

import React from 'react';

class navbar extends React.Component{
   render(){
     return(
       <div>...</div>
     )
   }
}

export default navbar;

Way 2:

import React,{Component} from 'react';

class navbar extends Component{
   render(){
     return(
       <div>...</div>
     )
   }
}

export default navbar;

Make sure to include this in your navbar component.

Here is a fully functional code snippet for reference.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Discovering the clicking actions on PDF elements within an HTML environment

I am currently working on developing a web application that involves rendering various pdf objects. My main goal is to be able to detect the position of a click inside the pdf container. However, it seems like the OnClick event is not functioning as expe ...

Building a dynamic web form using JSP and Angular for the JEE platform

I am currently working on a banking project using JSP and angular 1.3.2. I have encountered an issue with a form where, upon blurring after typing the customer number, the customer's information such as name and address should be displayed if the cust ...

Pause the YouTube video when the modal is closed, following an AJAX load

I'm facing an issue with a small script that is supposed to stop YouTube videos from playing once their modals are closed. The problem arises when the HTML content is loaded through AJAX. Here is the script: $('#panelModal-1').on(&apos ...

Issue with Ajax request not adding content to div

My issue with ajax $('#searchform').submit(function(event) { $.ajax({ type: "get", url: "/list", dataType: "text", data: $("#searchform").serialize(), beforeSend: function() { $(".se-pre-con").show(); }, ...

Consolidating Arrays in MongoDB: A Handy Function

I have a substantial dataset containing documents that sometimes reference each other and sometimes do not. Before I can perform mapreduce operations based on these cross-references, I need to ensure that the array of cross-references is consistent for eve ...

Encountered Issue: Input of type 'number' cannot be assigned to an argument expecting type 'string'

In my React TypeScript project, I am utilizing the React Player library and a Material UI volume slider to control the volume of the video player. The volume value ranges from 0 to 1, so I created a function to calculate the value based on the slider inpu ...

Insert round sphere shapes in the corners of the cells on the Table

I am working on a GUI representation table that features cells with spheres in the corner. Additionally, the footer contains a plus symbol (font-awesome icon) in the corner. I have attempted to place the sphere so that it aligns only inside the cell. Here ...

How can you trigger a 'hashchange' event regardless of whether the hash is the same or different?

Having a challenge with my event listener setup: window.addEventListener('hashchange', () => setTimeout(() => this.handleHashChange(), 0)); Within the handleHashChange function, I implemented logic for scrolling to an on-page element whil ...

Accessing Information from the Service

Here's the code snippet for my controller and service: var app = angular.module('myApp', ['ui.bootstrap']); app.service("BrandService", ['$http', function($http){ this.reloadlist = function(){ var list; $http.g ...

Utilizing the power of `mutateAndGetPayload` to yield

I have been attempting to use the mutateAndGetPayload function of a mutationWithClientMutationId to return a Promise, but I am encountering an issue where the request remains pending and eventually times out without receiving a response. Here is some exam ...

What could be causing me to receive no results?

Currently, I am expanding my knowledge in JavaScript, Ajax, and NodeJs. My current project involves creating a webpage that can display a string retrieved from the server. The server-side code is as follows: var express = require('express'); v ...

Utilizing the power of React and Flux alongside xhr for handling routing and caching

Questioning opinions and seeking validation on understanding Flux. In my quest to comprehend Flux with strictness, I delved into the realm of XHR calls, websockets, external stimuli, routing mechanisms, and more. Various resources such as articles, interv ...

Problem with a for loop: retrieve only the final item

Using the fileReader to read the content of selected files and appending it to the DOM creates a paragraph element for each file. However, when attempting to store each file's content in local storage, only the last item is being saved. What could be ...

Is there a resource available that can help me create a jquery/ajax image slider/carousel similar to this?

One thing that really caught my eye is how cnn.com has implemented this feature. I think it would be a great addition to the website I'm currently working on. I particularly like the page numbering, as well as the first, previous, next, and last butto ...

How can I use jQuery to identify the numerical range within class/td/div elements and modify their CSS styles?

1# I need assistance in changing the CSS properties of a TD, Class, and div using a selector that specifies a specific number range. Specifically, I am looking to modify the css of torrent results with a seed count between 250-25000. Any torrents with a se ...

Converting a string to a date in Vue.js

My API is returning a date string in the format: DD_MM_YYYY_hh_mm__ss I am aiming to present the string in the format: DD/MM/YYYY hh:mm:ss What is the best method to achieve this? ...

onChange does not trigger useEffect

In the Order Content component, I am receiving some products as props to be used in a select component. When a product is selected in the dropdown, it triggers the rendering of Summary and Product components. In these components, the user can choose the ...

Why is the date loaded in the link function not binding to the newly created HTML in the directive?

How can I write a directive to handle repetitive code? In this example, I need to load data from attachmentUsageService and generate HTML. The service successfully loads the data in the first step, but the data is not bound to the created HTML element. See ...

Is it possible to save data to a file using React?

As I work on a React web application, the need has arisen to store crucial user data on the client side in a stable manner. Due to the requirement of data stability and the constraint against using Indexed DB, I am considering storing the data as JSON in ...

Is there a way to remove elements from a canvas using jquery?

I implemented a jquery code snippet from this source to add a falling confetti effect on my webpage. You can view the demo page by clicking here. The code works smoothly, and I noticed that I can use the start and stop functions to control the updates. Ho ...