Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

218
Visualizações
React Router <Link> changes URL but does not automatically render component

I'm attempting to use React Router to allow a button in my NavBar to change the view when a button is clicked. Specifically, I want an "Upload" button to change the URL to /upload and render the UploadPage component.

Currently, the URL will change, but the view does not automatically render, and the 'old' component is still visible. I need to manually reload the page or go to the URL directly for the view to load. How can I stop this behaviour so that it automatically renders without manual reload?

App.js:

import { Component } from "react";
import Home from "../src/components/Component/Home/Home";
import Header from "./components/Component/Header/Header";
import UploadPage from "./components/Component/VideoUpload/VideoUpload";
import { BrowserRouter, Switch, Route } from "react-router-dom";

class App extends Component {
  render() {
    return (
      <BrowserRouter>
        <Header />
        <Switch>    
          <Route path="/" exact component={Home} />
          <Route
            path="/video/:videoId"
            component={Home}
            render={(routerProps) => <Home {...routerProps} />
          />
          <Route path="/upload" component={UploadPage} />
        </Switch>
      </BrowserRouter>
    );
  }
}

export default App;

Header.js (This is a snippet of just the button in the NavBar)

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

const Header = () => {
    return (
        //NavBar code here...

        <Link to="/upload">
        <button>
            <img draggable="false" src={uploadIcon} alt="upload-icon" />
            UPLOAD
        </button>
        </Link>
    );
};

VideoUpload.js

import React from "react";

const VideoUpload = () => {
  return (
    <section className="uploadContainer">
      <section className="uploadContainer__titleContainer">
        <h2 className="uploadContainer__title">Upload Video</h2>
      </section>
    </section>
  );
};

export default VideoUpload;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

After a lot of trial and error and research, I found that in my index.js:

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>
);

Should be changed to:

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

From my tests, <BroswerRouter> isn't necessary here and <App /> doesn't need to be wrapped for it to work, but, I'm keeping it wrapped just in case.

This fixes the issues I was experiencing.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda