Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error refresh router react router

Tags:

react-router

When I click on the link that contains url products/new I can access the new product page and it works fine, but if I refresh the page it returns Uncaught SyntaxError: Unexpected token <.

How can I resolve this?

Page product new

import React from 'react';

class ProductNew extends React.Component {
  constructor(props) {
    super(props);
  }
  render(){
    return (
      <div>
        <div><h1>ProductNew</h1></div>
      </div>
    );
  }
}

module.exports = ProductNew;

page products

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

class Products extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <div>
        <div><h1>Products</h1></div>
        <Link to="/products/new">new</Link>
      </div>
    );
  }
}

module.exports = Products;

Page App

render((
  <Router history={createBrowserHistory()}>
    <Route path="/" component={Layout}>
       <IndexRoute component={Home} />
       <Route path="/products" component={Products}/>
       <Route path="/products/new" component={ProductNew}/>
    </Route>
  </Router>
), document.getElementById('app'));
like image 748
Jorge Roberto Avatar asked Dec 03 '15 10:12

Jorge Roberto


People also ask

Does React Router reload 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.

What is BrowserRouter in React?

BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.

Does React Router lazy load?

Lazy Loading on route level with React Router is a powerful feature. Usually a client-side rendered React applications comes as one bundle from a web server. However, when enabling lazy loading, the bundle is split into smaller bundles.


1 Answers

I had a similar problem, my App.js path were relative. So when I loaded the app page from home path everything worked well, but when I tried to load from a link like yours "/product/new", I received an error like yours. When I put an absolute path, it worked fine.

I was using webpack to bundle the file and webpack-dev-server to run the development env and there is no physical files when you use webpack-dev-server hot loader (by default), it's a thing there is easy to fall in.


Edit: this guy's question has 2 updates on it, with the same problem as you and has a better answer/info than that I gave to you.

like image 81
Felipe Ramos Avatar answered Oct 13 '22 11:10

Felipe Ramos