Sathya Vikram Sathya Vikram - 3 months ago 17
React JSX Question

How to open index.html page when page in refresh in SPA Application using react-router

I am using React and React-router in my SPA. When user navigates from one view to another, URL gets appended with hash (e.g #/broker/5e1f75c6-5a62-4d60-860c-1dd0d5ff8644?_k=w5wn3g) and this is expected and works fine for all views.

However when user refreshes page on any view, I want React-router to redirect to Index Route irrespective of view the user in. Is there any configuration I can do in React-router to setup this?

Right now React-router tries to navigate to matching route view. I just want to redirect to Index route/index.html without any server side redirects. Any suggestions ?

Answer

You can use the replace (or push) function of react-router. Call your history singleton's replace method after you set up the history.

Example

import { hashHistory } from 'react-router';

hashHistory.replace('/');

References

https://github.com/reactjs/react-router/blob/master/docs/API.md