1

I've tried to set up routing, but as stated in title URL in browser does change but data doesn't load.

My App.js is defined as:

import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import React from 'react';
import Navbar from './components/Navbar';
import Addbook from './components/Addbook';
import Listbook from './components/Listbook';

function App() {
  return (
    <Router>
      <div>
        <Navbar />
        <Switch>
          <Route exact path="/book/add">
            <Addbook />
          </Route>
          <Route exact path="/book/list">
            <Listbook />
          </Route>
        </Switch>
      </div>
    </Router>
  );
}

export default App;

In Navbar I declared it as:

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

<ListItem
  button
  selected={selectedSection === 'Books'}
  onClick={() => handleSectionSelect('Books')}
>
  <ListItemIcon>
    <BookIcon />
  </ListItemIcon>
  <ListItemText primary="Books" />
  </ListItem>
    {selectedSection === 'Books' && (
      <>
        <ListItem button component={Link} to='/book/add'>
          <ListItemIcon>
            <BookmarkAddIcon />
          </ListItemIcon>
          <ListItemText primary="Add Book" />
        </ListItem>
        <ListItem button component={Link} to='/book/list'>
          <ListItemIcon>
            <ManageSearchIcon />
          </ListItemIcon>
          <ListItemText primary="Manage Books" />
        </ListItem>
      </>

I've tried to omit Switch, declare routes other ways. Adding refreshonClick as action to button. However none of it works.

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
Mmot
  • 11
  • 2
  • A couple questions, (1) what version of react-router-dom are you using, (2) what data are you referring to, since there is no data being displayed in either of the code snippets (are you referring to the components not rendering?), (3) in the navbar snippet you gave, is the boolean statement that starts with ```selectedSection === "Books" ``` supposed to have another statement that got cut off, thus the ```&& (```? – Ethan Krug Jun 05 '23 at 14:40
  • @EthanKrug 1. I'm using: "react-router-dom": "^5.2.0" 2. I'm referring to component not being renered. I have views under: import Addbook from './components/Addbook'; import Listbook from './components/Listbook'; First one is working as a post, second one as a get. 3. After: > There is: )} to close the statment. Below that I have other with cases like that. Everything is inside AppBar then Drawer. If I can provide more informations to you, please say so. I'm pretty new and not sure what might be lacking. Thanks for your help aswell. BR – Mmot Jun 05 '23 at 15:02
  • So the GET component (Listbook) isn't displaying properly, is what I'm hearing. Have you checked the console if there were any errors like 404 or 403 when trying to get the data? Also, it might help to try and print out the data in the console for debugging purposes. – Ethan Krug Jun 05 '23 at 15:18
  • Is the URL changing but the other component isn't being mounted/rendered? You say are using `react-router-dom@5.2.0`, do you happen to also be using `react@18` and render the app into a `React.StrictMode` component? Does this help answer your question? https://stackoverflow.com/a/71833424/8690857 – Drew Reese Jun 05 '23 at 15:23
  • I mean, it's displaying correctly after I refresh page. Main issue is: I press the button with link to "/book/add". After that my url in browser switches from: http://localhost:3000/ to http://localhost:3000/book/add. However none of the data is being displayed until i refresh website. Then the view is loading correctly. @DrewReese Yes, I'm using "react-dom": "^18.2.0", Index: const root = ReactDOM.createRoot(document.getElementById('root')); root.render( ); I omited imports due to characters limit. – Mmot Jun 05 '23 at 15:23
  • @DrewReese THANK YOU VERY MUCH! After updating router-dom to higher version and changing index.js it's working properly. Thank you very much. – Mmot Jun 05 '23 at 15:31

0 Answers0