Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router deep linking

This is my first time using React and React Router, and I am running into some deep linking issues. I've build a simple SPA, and with the help of React Router, I can navigate to mysite.com/work, mysite.com/about, and mysite.com/work/:projectid. I have no problem navigating to these pages if I start at the root of the site (mysite.com and clicking on 'Work', for example). The problem I am running into is deep linking (manually entering mysite.com/about or refreshing the page on mysite.com/about). This all works on my localhost but not on the hosted site (which, btw, is hosted on GitHub pages).

Is this because I am using GitHub pages to host my site? Or is this an issue with React Router?

Here is part of main.js:

var routes = (
    <Router history={history}>
        <Route path="/" component={App}>
            <IndexRoute component={Work} />
            <Route path="/work" component={Work} />
            <Route path="/work/:id" component={ProjectDetail} />
            <Route path="/about" component={About} />
        </Route>
        <Route path="*" component={NotFound} />
    </Router>
);

ReactDOM.render(routes, document.getElementById('main'));
like image 402
user2909019 Avatar asked Dec 29 '15 19:12

user2909019


People also ask

How do you deep link in react?

There are two ways to handle Deep Linking in a React Native app: Without navigation: by invoking React Native's core library via JavaScript and directly calling Linking . You can learn more about this in React Native's official documentation.

Is react Router deprecated?

This feature has been deprecated because the new structure of Routes is that they should act like components, so you should take advantage of component lifecycle methods instead.

Is react Router v6 stable?

React Router v6 received a stable release on November 3, 2021. This version is half the size of the previous one and has much better support for hooks.

How do you link a Router in react JS?

To add the link in the menu, use the <NavLink /> component by react-router-dom . The NavLink component provides a declarative way to navigate around the application. It is similar to the Link component, except it can apply an active style to the link if it is active.


1 Answers

You can traverse the site from your root since the routing is all handled by react-router which loaded from your root site's js files.

There is no html file located in /about/index.html. gh-pages hosts static sites, and if you go to mysite.com/about without having the html file, it will probably give you a 404.

If you're using webpack, try using https://github.com/markdalgleish/static-site-generator-webpack-plugin.

like image 137
oobgam Avatar answered Sep 21 '22 14:09

oobgam