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.

Route Transitions

See original GitHub issue

How would I transition from one route to another after an action is performed? I have seen transitionTo() being used when a RouterContainer is created, but what is the equivalent in preact? I am also looking at securing a route until someone logs in. I have seen the following for React, is there also an equivalent for the following?

static willTransitionTo(transition, params, query, callback) {
        if (!AuthStore.loggedIn()) {
            // go over to login page
            transition.redirect('/login', null, {
                redirect: transition.path
            });

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
developitcommented, May 4, 2016

To redirect, you can use the route() method. The static willTransitionTo method from react-router is a little awkward (I believe they are looking at alternative ways to implement such things). My suggestion would be to use a high-order component that does the session check and conditionally renders the actual route component only if there is a valid session.

Here’s a simple example:

import { route } from 'preact-router';

const AuthenticatedRoute = ({ route }) => {
  if (!AuthStore.loggedIn()) {
    // redirect to login:
    setTimeout( () => route('/login'), 1);
    // don't render the route:
    return null;
  }
  // we're logged in, so render the route:
  return route;
}

You could also do the loggedIn() check asynchronously by firing it from componentDidMount() and setting the returned Boolean as a property in a stateful component.


Here’s a more complete example, using a generalized <AuthenticatedRoute /> high order component to wrap a given route in an auth check like you described:

import { Router, route } from 'preact-router';

class AuthenticatedRoute extends Component {
  componentWillMount() {
    let { check, login } = this.props;
    this.allowed = check();
    if (!this.allowed) route(login || '/');
  }
  render({ check, login, route:Route, ...props }) {
    return <div>{ this.allowed && <Route {...props} /> }</div>;
  }
}

const LoginRoute = () => <form>login form here</form>;

const AccountRoute = () => <div>secret account page</div>;

const Demo = () => (
  <Router>
    <LoginRoute path="/login" default />
    <AuthenticatedRoute
      path="/account"
      login="/login"
      check={AuthStore.loggedIn}
      route={AccountRoute} />
  </Router>
);
1reaction
donadleycommented, May 31, 2016

Hey, thanks for fixing the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Transitions - Vue Router
In order to use transitions on your route components and animate navigations, ... If you want each route's component to have different transitions, ......
Read more >
Route transition animations - Angular
When a user navigates from one route to another, the Angular router maps the URL path to a relevant component and displays its...
Read more >
route_transitions | Flutter Package - Pub.dev
This package has been rewritten to use friendly route functions to navigate even with your favourite route transition and ability to build custom...
Read more >
4 Awesome Examples of Vue Router Transitions - LearnVue
Vue Router transitions are a quick and easy way to add some flair to Vue app. They allow you to add smooth animations/transitions...
Read more >
Animate a page route transition - Flutter documentation
This recipe shows how to transition between routes by animating the new route into view from the bottom of the screen. To create...
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