Programmatically navigate using React router

0 votes

With the help of the react-router I am able to use the element with the link to create more links which are then handled by the route reactor. I have noticed that it calls this.context.transitionTo(...). internally. For me to do a navigation not with the help of a link but with the help of a dropdown selection, how could I use code to do so? What does this.context mean? I have noticed the Navigation mixin but in order to do this without the help of mixins, what can I do?

Feb 8 in Others by Rahul
• 8,980 points
13 views

1 answer to this question.

0 votes

To answer your question, you should try the React Router v5.1.0 with hooks. An update for a new useHistory hook has been introduced in the React Router >5.1.0 if what you are using is React>16.8.0 along with the components that are functional, then please use the given lines of code:-

import { useHistory } from "react-router-dom"; 

function HomeButton() { 
const history = useHistory(); 

function handleClick() { 
history.push("/home");

} 

return ( 
<button type="button" onClick={handleClick}> 
Go home 
</button>
); 
}

When coming to the use of the React Router v4, there are about three various approaches which can be implemented into your routine of programming within components such as using the withRouter higher order, using the composition and rendering a <Route> and finally to use the context. The React Router is nothing but a wrapper which is around the history library in which the history handles the interaction with the internet's window.history using the hash histories. Over this, it also provides the storage history which helps environments with an absence of global history. For testing units with Node and react-native which is used for mobile application development are areas where this is beneficial. Push and Replace are the methods for navigation of a history instance. If the history of the visited locations array is taken into consideration, then the array will have a new location added with the help of push and replace will replace the current location in the array with a new one. Normally, while navigation, the push button will be used. The <BrowserRouter>, <HashRouter>, and <MemoryRouter> components will make a browser, hash and storage instances for you in React Router v4. The router also makes methods of the history instance which is related to the router available which you are using under the router object.  In other words, by using the withRouter higher-order component, it will inject the history object as a part of the component which enables access to you and the push and replace methods without having to deal with the context. Refer below for the example:-
 

import { withRouter } from 'react-router-dom' 
// this also works with react-router-native 

const Button = withRouter(({ history }) => ( 
<button
type='button' 
onClick={() =
> { history.push('/new-location') }} > 

Click Me! 
</button> 
)
)

Another method used is to use the composition and render a <Route> as the <Route> component is not only for the purpose of simply matching locations. You can also render a route without a path and it will always match the current location. The <Route> component passes the same props as withRouter, so you will be able to access the history methods through the history prop using the following code:-
 


import { Route } from 'react-router-dom' 

const Button = () => ( 
  <Route render={({ history}) => ( 
  <button 
    type='button' 
    onClick={() = > { history.push('/new-location') }} 
> 
Click Me! 
</button> 

) } /> 

)
Finally, using the context* model as the React’s Context API is more stable as compared to v16 as displayed below:-

const Button = (props, context) => ( 
    <button 
      type='button' 
      onClick={() => {
  // context.history.push === history.push 
  context.history.push('/new-location') 
}} 
> 
Click Me! 
</button>
) 

  // you need to specify the context type so that it 
  // is available within the component 

Button.contextTypes = { 
history: React.PropTypes.shape({ 
push: React.PropTypes.func.isRequired 
})

}
answered Feb 8 by Soham
• 8,730 points

Related Questions In Others

0 votes
0 answers

Attempted import error: 'Switch' is not exported from 'react-router-dom'

Code: import React from 'react'; import './App.css'; import NavBar from ...READ MORE

May 15 in Others by Kichu
• 16,850 points
55 views
0 votes
1 answer
0 votes
1 answer

Using/Handling colon inside a JSF generated HTML element ID in CSS selector

Yes, you can.  Just Backslash (\) the colon.  Like ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,240 points
1,612 views
0 votes
1 answer

Using unserialize in PHP throws the same error repeatedly

I googled 'Node no longer exists', and ...READ MORE

answered Nov 14, 2018 in Others by DataKing99
• 8,240 points
781 views
0 votes
1 answer

How can I remove a port from url for node app using nginx

If you run your node server on ...READ MORE

answered Apr 10, 2018 in DevOps on Cloud by ajs3033
• 7,280 points
2,621 views
0 votes
3 answers

ReactJS vs Angular Comparison: Which is better?

One picture, many facts How can you compare ...READ MORE

answered Aug 11, 2018 in Events & Trending Topics by slayer
• 29,310 points
403 views
+2 votes
4 answers
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP