Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  29] [ 7]  / answers: 1 / hits: 36848  / 7 Years ago, thu, july 6, 2017, 12:00:00

I'm learning React. I have page with 4 subpages and i use React Router to go through those pages. Everything works fine except for reloading page. When i go from page home to about or other it's ok, but when i refresh page then it render again page about for a second and then it change page to home (home is default/first page).I want it to stay at that page which is currently rendered, not go back to home after every refresh.
There is my code in index.js file where i render whole app and use router:



<Provider store={store}>
<Router path=/ history={browserHistory}>
<Route path=/home component={App}>
<Route path=/ component={Home}/>
<Route path=/allhotels component={AllHotels}/>
<Route path=/addhotel component={AddHotel} />
<Route path=/about component={About} />
</Route>
<Route path=/signin component={SignIn} />
<Route path=/signup component={SignUp} />
</Router>
</Provider>, document.getElementById('root')


In App i have Navigation and there i render rest of conent from Home, AllHotels etc.



There is code from App.jsx



class App extends Component {

render() {
return (
<div className=app-comp>
<Navigation />
<div> {this.props.children}</div>
</div>
)
}
}


I also attach gif which shows my problem.



https://gifyu.com/image/boMp



In backend i use Firebase if it's important.



Thanks in advance.


More From » reactjs

 Answers
10

Check that firebase does not interfares with the client side routes :P



You can use Index routes to achieve this.



You have your navigation i.e the layout of all pages in your app component so make it the root route.



Then you want your home route to be your default route so make it your Index route.



You need to import IndexRoute from react-router package (from which you import Route).



Add this-



import { Router, Route, IndexRoute } from 'react-router';


and then make your routes like below.



Use this-



<Provider store={store}>
<Router history={browserHistory}>
<Route path=/ component={App}>
<IndexRoute component={Home} />
<Route path=/home component={Home}/>
<Route path=/allhotels component={AllHotels}/>
<Route path=/addhotel component={AddHotel} />
<Route path=/about component={About} />
</Route>
<Route path=/signin component={SignIn} />
<Route path=/signup component={SignUp} />
</Router>
</Provider>, document.getElementById('root')

[#57202] Monday, July 3, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jimmieo

Total Points: 515
Total Questions: 102
Total Answers: 110

Location: Kazakhstan
Member since Mon, Sep 26, 2022
2 Years ago
;