khushboo29
khushboo29

Reputation: 916

React routing with express is working on page refresh

My project using express for server and React for frontEnd. Routes are like this

 <Router >
    <Switch>
        <Route exact path="/" component={HomeContainer} />
        <Route path="/women" component={SectionContainer} />
    </Switch>
</Router>

To serve these routes my server js has

 server.get('*', function(request, response) {
  response.sendFile(path.resolve(__dirname, '../public', 'index.html'));
 });

Page url http://localhost:3000/women is working only on page refresh, first time click on url is just changing the browser url with no page update. But on page refresh it is working perfectly fine. Please suggest what i am missing.

Upvotes: 0

Views: 856

Answers (1)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

I was having similar issue. I found HashRouter helpful than the BrowserRouter:

import { HashRouter as Router } from 'react-router-dom'

Using HasRouter will be working fine as it keeps state on every history data changes.

Upvotes: 1

Related Questions