How to programmatically navigate using React Router? With react-router I can use the Link element to create links which are natively handled by react router. I see internally it calls this.context.transitionTo(...). I want to do a navigation. Not from a link, but from a dropdown selection (as an ...
The error comes from your dependency relying on an obsolete version of SSL, so you have two good, and two questionable-at-best options: 1. Try to just reinstall your dependency Delete your node_modules folder and rerun npm install. If your dependency relies on compiling against whatever version of NRead more
The error comes from your dependency relying on an obsolete version of SSL, so you have two good, and two questionable-at-best options:
1. Try to just reinstall your dependency
- Delete your node_modules folder and rerun
npm install
. If your dependency relies on compiling against whatever version of Node you have installed, this may immediately fix the problem. This is the least likely solution to work, but may fix the problem without any “real” work on your part so is always worth trying.
2. Update your dependency
- Almost all dependencies with this problem have a newer version available that you can install instead. Find out which version of your dependency corresponds to after Node 18 became the LTS version of Node, band uplift your dependency to that version.
This is, really, the only proper solution: update your dependencies, because just like Node.js itself, they can leave your project vulnerable to attacks and exploits.
3. Downgrade to Node.js v16.
- You can downgrade Node itself so that you’re using a version that uses the old, insecure, version of LibSSL. That doesn’t “solve” the problem of running insecure and potentially exploitable code, of course, but your code will at least run.
(You can either do that using the official Node installers, or you can use something like nvm
. For Windows, use nvm-windows
.)
This is, obviously, a bad idea. As is the next one:
4. Tell Node to use the legacy OpenSSL provider
On Unix-like (Linux, macOS, Git bash, etc.):
export NODE_OPTIONS=--openssl-legacy-provider
On Windows command prompt:
set NODE_OPTIONS=--openssl-legacy-provider
On PowerShell:
$env:NODE_OPTIONS = "--openssl-legacy-provider"
When Node 18 had just become the active LTS options 1 and 2 weren’t really available, but for anyone still finding this answer, 3 and 4 should no longer be considered serious options in any way.
See less
The useHistory() hook is now deprecated. If you are using React Router 6, the proper way to navigate programmatically is as follows: import { useNavigate } from "react-router-dom"; function HomeButton() { const navigate = useNavigate(); function handleClick() { navigate("/home"); } return ( <buttRead more
The
useHistory()
hook is now deprecated. If you are using React Router 6, the proper way to navigate programmatically is as follows:There is a new
useHistory
hook in React Router >5.1.0 if you are using React >16.8.0 and functional components.With v4 of React Router, there are three approaches that you can take to programmatic routing within components.
withRouter
higher-order component.<Route>
context
.React Router is mostly a wrapper around the
history
library.history
handles interaction with the browser’swindow.history
for you with its browser and hash histories. It also provides a memory history which is useful for environments that don’t have a global history. This is particularly useful in mobile app development (react-native
) and unit testing with Node.A
history
instance has two methods for navigating:push
andreplace
. If you think of thehistory
as an array of visited locations,push
will add a new location to the array andreplace
will replace the current location in the array with the new one. Typically you will want to use thepush
method when you are navigating.In earlier versions of React Router, you had to create your own
history
instance, but in v4 the<BrowserRouter>
,<HashRouter>
, and<MemoryRouter>
components will create a browser, hash, and memory instances for you. React Router makes the properties and methods of thehistory
instance associated with your router available through the context, under therouter
object.1. Use the
withRouter
higher-order componentThe
withRouter
higher-order component will inject thehistory
object as a prop of the component. This allows you to access thepush
andreplace
methods without having to deal with thecontext
.2. Use composition and render a
<Route>
The
<Route>
component isn’t just for matching locations. You can render a pathless route and it will always match the current location. The<Route>
component passes the same props aswithRouter
, so you will be able to access thehistory
methods through thehistory
prop.3. Use the context*
But you probably should not
The last option is one that you should only use if you feel comfortable working with React’s context model (React’s Context API is stable as of v16).
1 and 2 are the simplest choices to implement, so for most use cases, they are your best bets.
See less