Router: Easier access to route() in class components
See original GitHub issueIs your feature request related to a problem? Please describe.
I want to switch to preact-iso’s router but find it’s very cumbersome to get a hold of its route()
function in class components (as I can’t use useRoute()
).
I’ve managed to do it, but it’s not pretty:
import { Component } from 'preact';
import { Router } from 'preact-iso';
class C extends Component {
render() {
return (
<Router.Provider.ctx.Consumer>
{ ctx => (
<div onClick={ event => ctx.route('/some/place') }>Click me!</div>
)}
</Router.Provider.ctx.Consumer>
);
}
}
Describe the solution you’d like
In preact-router, I could just do:
import { route } from 'preact-router'
and then use route()
pretty much anywhere.
My guess is that preact-iso probably switched to useRoute()
for a reason and that the import { route }
isn’t entirely ‘compatible’ anymore, but I would appreciate it if there was a better way to get a hold of it 😃
Thanks!
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
React-Router routing to class components
I am trying to route to a class component but it gives me an error. When I change the component to a functional...
Read more >How To Handle Routing in React Apps with React Router
React Router lets you split out child routes directly in the component, which means you don't need to have the whole list in...
Read more >React Router DOM: How to handle routing in web apps
A router allows your application to navigate between different components, changing the browser URL, modifying the browser history, and keeping ...
Read more >withRouter
You can get access to the history object's properties and the closest <Route> 's match via the withRouter higher-order component. withRouter will pass...
Read more >React Router 6 Tutorial #2 - Route Components - YouTube
In this React Router 6 tutorial series you'll learn what's new to React Router 6 and together we'll refactor a eact site that...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@rejhgadellaa ah! sorry, I messed up the example for
#2
- it should becontextType
(singular):or, if you’d like to avoid the getter:
@rejhgadellaa my suggestion here would be to move LocationProvider out of all of your components, and have one small root component that only renders the providers for your app: