Skip to content

Miss producing strange behavior when using MatchWhenAuthorized #4150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trappar opened this issue Nov 7, 2016 · 3 comments
Closed

Miss producing strange behavior when using MatchWhenAuthorized #4150

trappar opened this issue Nov 7, 2016 · 3 comments
Labels

Comments

@trappar
Copy link

trappar commented Nov 7, 2016

Using 4.0.0-alpha.5, and in the situation described in the title, like...

<Match pattern="/login" component={LoginPage}/>
<MatchWhenAuthorized pattern="/secured" component={SecuredPage}/>
<Miss component={NoMatch}/>

I click a link to go to /secured and on that page in addition to the normal page content I'm seeing the NoMatch component being rendered. I then click on a link again to go to /secured and the NoMatch component disappears.

I would assume this is because the actual Match component isn't a direct sibling of the Miss component when using MatchWhenAuthorized, but then how is it working after a second link click?

Here's my MatchWhenAuthorized component (using MobX)

import React from 'react';
import {Match, Redirect} from 'react-router';
import {observer} from 'mobx-react';

export default observer(['user'], MatchWhenAuthorized);

function MatchWhenAuthorized({ component: Component, user, ...rest }) {
  return (
    <Match {...rest} render={props => (
      user.loggedIn ? (
        <Component {...props}/>
      ) : (
        <Redirect to={{ pathname: '/login', state: { from: props.location } }}/>
      )
    )}/>
  );
}
@alisd23
Copy link
Contributor

alisd23 commented Nov 7, 2016

Yep this is a bug. The original issue was #4035, and I think we thought it was fixed, but clearly not.
It's caused by that mobx observer HOC wrapper implementing shouldComponentUpdate I think, as the same problem exists with a Redux connect HOC.

There's also a failing test PR being tracked at #4047.

@alisd23 alisd23 added the bug label Nov 7, 2016
@trappar
Copy link
Author

trappar commented Nov 7, 2016

I modified my MatchWhenAuthorized component so it no longer makes use of observer and instead I'm passing the user in directly. That fixed the issue, so you're definitely right about the cause.

So for anyone who finds this and is looking for a solution - that will work for now.

@ryanflorence
Copy link
Member

the sCU issues are on my mind in other issues too, so I'm going to close this. There will be docs/examples on how to get around it, and what to watch out for (or maybe we can avoid the problem altogether).

@lock lock bot locked as resolved and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants