Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
187 views
in Technique[技术] by (71.8m points)

javascript - React Router URL works but not Component

React Router changes the URL but the component is not updated. I checked many solutions from StackOverflow but it still didn't work. Here is my code: App.js:

function App() {
  return (
        <div className="App">
                <Header />
                <Sidebar />
          <Switch>
              <Route exact path={'/'} component={withRouter(Dashboard)} />
              <Route path={'/invoices/:objectId'} component={withRouter(Table)} />
          </Switch>
        </div>
  );
}

export default App;

SideNavigation:

    function Sidebar(){

    return(
        <div className="sidebar-wrapper">
            <div className="sidenav">
                <NavLink to={'/'}>Dashboard</NavLink>
                <NavLink to={'/invoices/1'}>Test1</NavLink>
                <NavLink to={'/invoices/2'}>Test2</NavLink>
                <NavLink to={'/invoices/3'}>Test3</NavLink>
            </div>
        </div>
    );
}
export default Sidebar;

index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter } from "react-router-dom";
import './static/index.css';
import App from './App';

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

I'm barely new to React and open to any other improvements on my code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why have you wrapped your components in a withRoute when they are already wrapped in a Route... /> component? I see no reason to do that.

Please read this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...