In React JS, when the navbar button is clicked, the URL is updated but the content does not refresh

Upon clicking the button, the URL changes but the content remains the same. Interestingly, if I use an <a> tag instead of a <Link> tag, the page loads and the content updates as well. However, when using the <Link> tag, the data does not load even though the URL value is updated. Can you please provide a solution for this issue?

App.js

import "./App.css";
import "../node_modules/bootstrap/dist/css/bootstrap.css";
import Home from "./component/pages/Home";
import Contect from "./component/pages/Contect";
import About from "./component/pages/About";
import Navbar from "./component/layout/Navbar";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import React from "react";

function App() {

  return (
    <Router>
     
<Navbar title="title1" />
      
<Switch>
        <Route exact path="/Contect" component={Contect}></Route>
        <Route exact path="/" component={Home}></Route>
        <Route exact path="/About" component={About}></Route>
      </Switch>
    </Router>
  );
}

export default App;

Navbar.js

import React from "react";
import { Link } from "react-router-dom";
import {} from "react-router-dom";

const Navbar = (props) => {
  return (
    <nav class="navbar navbar-expand-lg navbar-light bg-dark">
      <div className="container">
        <Link class="navbar-brand bg-light" to="/contect">
          {props.title}
        </Link>

        <button
          class="navbar-toggler"
          type="button"
          data-bs-toggle="collapse"
          data-bs-target="#navbarSupportedContent"
          aria-controls="navbarSupportedContent"
          aria-expanded="false"
          aria-label="Toggle navigation"
        >
          <span class="navbar-toggler-icon "></span>
        </button>
        <div class="collapse navbar-collapse">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item active">
              <Link class="nav-link bg-light" to="/">
                Home
              </Link>
            </li>
            <li class="nav-item active">
              <Link class="nav-link bg-light" to="/About">
                About
              </Link>
            </li>
          </ul>
        </div>
      </div>
    </nav>
  );
};

export default Navbar;

Home.js

    import React from "react";
    
    const Home = () => {
      return (
        <>
          <div className="container">
            <div className="py-4">
              <h1> Home page </h1>
            </div>
          </div>
        </>
      );
    };
    
    export default Home;

About.js and Contect.js are similar to Home.js

Answer №1

<Router>  
  <Navbar title="title1" />   
  <Switch>
    <Route exact path="/" component={Home}/>
    <Route path="/Contect" component={Contect}/>    
    <Route path="/About" component={About}/>
  </Switch>
</Router>

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

Encountering difficulties in accessing files displayed by serve-index in Express

My Node.js server using Express seems to be working fine for displaying directory contents, but I'm running into an issue when trying to access individual files. After clicking on a file listed in the directory, I keep getting an error message that sa ...

Having trouble with my getJSON function, can't pinpoint the error in my code

After collaborating with some fellow stack users, I have developed the following: http://jsfiddle.net/9ywLq/ I am looking to integrate an external json file in order to achieve something similar to this: http://jsfiddle.net/RCB9M/ Currently, I am linki ...

Unexpected behavior observed in React TypeScript when using the useRef hook. TypeScript does not generate errors for incorrect ref types

I am encountering an issue with a simple react component. Even though TypeScript should throw an error, it does not when I use HTMLInputElement as the type for useRef hook and assign it to a div element. import { useRef } from "react" export de ...

Utilizing the initial entry in MongoDB exclusively

I have developed a unique adventure command that searches for a player's profile in a Mongo database to calculate various game metrics such as xp and coins. While my other database commands are working flawlessly, this particular command is causing an ...

Unable to run npm installation for the project

I'm facing an issue with my project when trying to run npm i. Below is the content of my package.json file: { "name": "gogo-react", "version": "4.2.3", "description": "Gogo - React Bootstra ...

What is the best way to save the values of a particular attribute from several documents?

I have a challenge where I need to extract the ObjectID values from multiple documents stored in a single collection and store them in a single array. I'm currently stuck on how to achieve this task. Below is what I've tried so far: app.get("/se ...

The slideUp and slideDown functions are not functioning properly

Whenever a user clicks on image-exp, I want description-exp to slide down while other description-exp slides up. I am currently using a Bootstrap template available at this link. To demonstrate the issue, I created a JSFiddle that can be accessed here. H ...

Clearing the canvas completely with CamanJS: a step-by-step guide

I need some help with my CamanJS photo editing app. Everything is working perfectly except for one issue - when a user uploads a new image and applies a filter, the canvas reverts back to the previously uploaded image. Even though I am using the revert() f ...

React and Express: Encounter a proxy error when trying to proxy the request for /total from localhost_errors

This question is a continuation of my previous inquiry on React.js: Axois Post stay on pending(but i am getting data) Below is the content of my package.json file { "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "ax ...

Uniform Grid spacing issue with React Material-UI version 5

In my grid layout, I have set the spacing to 2 with 2 text fields per row. However, I noticed that the spacing is only applied to the left of each text field and not at the end to the right of the second text field in each row. Is there a way to adjust th ...

gulp-watch does not monitor files that are newly created or deleted

Currently working on a task: gulp.task('assetsInject', function () { gulp.src(paths.index) .pipe(plugins.inject( gulp.src(paths.scripts, {read: false}), { addRootSlash: false } ...

Retrieving image source from JSON data using AngularJS

I am attempting to retrieve an image from an API using AngularJS. The approach I'm taking involves triggering the $http function with a link and attempting to extract the image URL from the JSON response to display it in the HTML. However, despite re ...

Utilizing ReactJs to inject CSS styles from the root level

This question may seem simple, but I have been struggling to find an answer even today. I am attempting to import index.css from main.tsx starting from the root. This should allow me to apply styles like: <div className="anyStyle" /> Curr ...

Issues encountered while retrieving API payload in Reactjs using Typescript

Having some trouble storing an API payload in state, as I keep receiving the following error message: Error: Objects are not valid as a React child (found: object with keys {page, results, total_pages, total_results}). If you meant to render a collection ...

When using a JavaScript object literal, is it more appropriate to use 'this' or the variable name for reference?

Struggling with object literal syntax and the use of "this" keyword was causing errors for me. However, when I replaced it with the object variable name itself, the issues disappeared. The error message I kept encountering was ''myFunction' ...

What is the best way to transform the request query id = ' [12eetftt76237,jhgasduyas7657] ' into an array of elements or strings like [12eetftt76237,jhgasduyas7657]?

Hey there, I am working on a project using hapijs and typescript. I have a requirement to send an array of IDs as parameters through the request URL. Here is an example of the URL: localhost:3444/?id='[askjajk78686,ajshd67868]' I attempted to u ...

What causes a gradual decline in performance when filling in a rectangle on a canvas?

I am currently developing an HTML5 game that uses a <canvas> element. I have noticed a strange drop in performance over time, particularly with the function responsible for painting the background on each frame. Here is a snippet of the code: functi ...

What is the best way to include multiple .css files in an HTML document?

I have a main page called homepage.html that currently links to styles.css (which creates a table). However, I also need homepage.html to link to styles12.css because it contains the CSS for tabs. Is there a way to reference two .css files? <title> ...

Requesting AJAX page yields a null $_GET parameter

Content loaded dynamically via AJAX methods. The base page is index.php, and profile.php is the page that gets loaded. The jQuery code snippet below has nothing to do with an HTML request: $('<a/>', {href: '?userID='+post[' ...

Error! Attempting to assign a value to property 'getName' of an undeclared object

<script> var Employee = new function(name) { this.name=name; } Employee.prototype.getName = function() { return this.name; } var PermanenetEmployee = new function(annualsalary) { this.annualsalary=ann ...