question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Router: Easier access to route() in class components

See original GitHub issue

Is 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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
developitcommented, May 29, 2022

@rejhgadellaa ah! sorry, I messed up the example for #2 - it should be contextType (singular):

class C extends Component {
  static get contextType() { return LocationProvider.ctx; }

  render() {
    // ...
  }
}

or, if you’d like to avoid the getter:

class C extends Component {
  render() {
    // ...
  }
}

C.contextType = LocationProvider.ctx;
0reactions
developitcommented, Jun 9, 2022

@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:

function C() {
  return (
    <ErrorBoundary>
      <Router />
    </ErrorBoundary>
  );
}

export default function App() {
  return (
    <LocationProvider>
      <C />
    </LocationProvider>
  );
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found