Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS - Route update link but not refresh page

Tags:

reactjs

I'm building a simple ReactJS page (my first one) and i'm having an issue with route. Link on browser bar is updated (no errors in console) but the page is not refreshed:

My App.js is:

return(
   <div id="container">
      <Dashboard>{this.props.children}</Dashboard>
   </div>
);

The Dashboard is the main page where the dynamic content is rendered on the middle

 return(
    <!-- code for navbar, sidebar etc -->
      ...
      <Link to="/memory">Memory</Link>
      ....
     <div id="page-wrapper" className="page-wrapper">
        {this.props.children}
     </div>
);

So i have created two small component (Home that is displayed at the beginning and Memory)

Home:

render() {
    return(
       <div className="row">
            HOMEPAGE
        </div>
    );
}

Memory:

  render() {
        return(
           <div className="row">
                MEMORY
            </div>
        );
    }

Router is very simple:

<Route component={App}>
    <Route path='/' component={Home}>
        <Route path='/memory' component={Memory} />
    </Route>
</Route>

When i go to my home server (localhost:3000) my HOMEPAGE is displayed but when i click on Memory link nothing happen... the url change but Memory component is not rendered...

UPDATE

Thanks to the reply i have had this is a working version:

Dashboard -> deleted

App.js

 return(
    <div id="container">
      <div className="content" id="wrapper">
        <Navigation />
        <div id="page-wrapper" className="page-wrapper" >
          {this.props.children || <Home />}
        </div>
      </div>
    </div>

In this way Navigation will contain only the part relative to navigation (top bar and left side bar).

Route has been changed:

<Route>
   <Route path='/' component={App}>
      <Route path='/memory' component={Memory} />
   </Route>
</Route>

Now when i go to home (localhost:3000) , the variable this.props.children is undefined and i render the Home component (thanks to {this.props.children || <Home />}).

In all other cases i render the right component provided by Link

like image 279
Mistre83 Avatar asked Mar 02 '16 12:03

Mistre83


People also ask

Does React router refresh the page?

react-router-dom allows us to navigate through different pages on our app with/without refreshing the entire component. By default, BrowserRouter in react-router-dom will not refresh the entire page.

How do I stop React from refreshing?

Use the preventDefault() method on the event object to prevent a page refresh on form submit in React, e.g. event. preventDefault() . The preventDefault method prevents the browser from issuing the default action which in the case of a form submission is to refresh the page.

How do you refresh the page without reload in React?

reload(false); This method takes an optional parameter which by default is set to false. If set to true, the browser will do a complete page refresh from the server and not from the cached version of the page.

Does React router Rerender?

React components automatically re-render whenever there is a change in their state or props. A simple update of the state, from anywhere in the code, causes all the User Interface (UI) elements to be re-rendered automatically.


1 Answers

Try this solution:

Routes:

   <Router>
    <Route path="/" component={App} >
      <Route path='/memory' component={Memory} />
    </Route>
   </Router>

And 'App' component:

    return (
      <div id="container">
        <Dashboard/>{this.props.children || <Home />}
      </div>
);

Don't forget, you need to connect 'Home' component in App. I can send you full working example.

like image 113
pgrekovich Avatar answered Nov 15 '22 11:11

pgrekovich