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.

willTransitionTo replacement in 1.x

See original GitHub issue

The current stable release 0.13.3 has a very convenient way to intercept routing transitions and do additional prevalidation. One can simply define a willTransitionTo hook and implement the validation logic and trigger a redirect if needed.

var Settings = React.createClass({
  statics: {
    willTransitionTo: function (transition, params, query, callback) {
      auth.isLoggedIn((isLoggedIn) => {
        transition.abort();
        callback();
      });
    }
});

The current 1.x beta releases removed that feature. The transition hooks are now only available on the Router and on the Routes itself but not on the components. So it is not possible to implement such a cross cutting concern by simply decorating the component. For now I use following workaround which is not very nice.

// decorate Component with onEnter hook.

function authenticated(Component) {
  const wrappedComponent = React.createClass({
    statics: {
      onEnter(next, transition) {
        if (!state.get('session.authenticated')) {
          transition.to('/login');
        }
      }
    },
    render() {
      return <Component {...this.props} {...this.state} />;
    }
  });

  return wrappedComponent;
}


// decorate the notAllowed Component
const decorated = authenticated(notAllowed);

// workaround onEnter delegates to statics onEnter of the component
<Route path="/notAllowed" component={decorated} onEnter={decorated.onEnter}/>

Another way to implement this would be to have own derived Route implementations like AuthRoute or so.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:26 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
anonymoshcommented, Jul 5, 2016

What were the reasons for this change? I really liked the way the components were responsible for handling / reacting on transition changes. It kept special handling in the context of the respective component.

Now i need to push this everything to the top Route configuration which seems to be an awful way of cluttering the file containing the config with unnecessary functions and imports.

The current auth example seems to simplified for the means of the example. Is there still a way to encapsulate the route transition hooks into a HoC?

0reactions
taioncommented, Jul 5, 2016

Not well. More importantly, you can’t bind to context in static methods – in onEnter you can e.g. close over a store.

Read more comments on GitHub >

github_iconTop Results From Across the Web

willTransitionTo replacement in 1.x · Issue #1388 - GitHub
One can simply define a willTransitionTo hook and implement the validation logic and trigger a redirect if needed. var Settings = React.
Read more >
Federal Reserve Issues Final Rule Implementing LIBOR Act
The Board-selected benchmark replacement identifies what benchmark replacement certain legacy LIBOR contracts will transition to on the LIBOR ...
Read more >
UIPageViewController transition 'Unbalanced calls to begin ...
Solved following these steps: 1- Declare a flag to indicate that the animation has finished or not:
Read more >
Rachel Maddow Returns To MSNBC, Will Transition ... - Forbes
Maddow told viewers that while she will shift to one night a week, she doesn't plan to take another extended hiatus away from...
Read more >
1 /16 2 /19 3 /11 4 /20 5 /18 6 /16
function Bit#(1) f_recursive#(Integer n) (Bit#(n) x); ... Using the replacement parts below, modify the circuit so that it can operate using a clock....
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