I am working on a sample reactjs application (in learning process). I have a page which list the list of users and a add button to add a new user.
When I click the add button I should navigate to the User Form to create the new user.
After I click the submit button in the user form it should navigate back to the first page where it should list the list of users along with the new user.
How to navigate between pages in react?
Apart from
browserHistory
, you can usehashHistory
also by importing it fromreact-router
.You do it with react router. Here is the react router tutorial.
Your list of users is the first page which is shown when you open the site, thus that is your index page and all other pages are routes.
Thus you can do something like this :
You can create a separate file with your routes :
Then your
index.js
should look something like this :Here you wrap it under
Router
which comes fromreact-router
, and there you pass history prop which you want to use and routes prop. You can usebrowserHistory
andhashHistory
. BrowserHistory shows cleaner urls. With hash history you have something likesomeurl.com/#/something
Now in your app you can do next :
{this.props.children}
renders all routes from routes file, because you have specified App component for the main route.On the add user button onClick event you can navigate to the add user form with browserHistory, thus :
And then on button click on add user form, the same process, you only need to navigate to the index route with
"/"
, thus :Hope this helps.