Skip to content
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

Replacing route component from within an onEnter() hook #3526

Closed
iotch opened this issue Jun 9, 2016 · 3 comments
Closed

Replacing route component from within an onEnter() hook #3526

iotch opened this issue Jun 9, 2016 · 3 comments

Comments

@iotch
Copy link

iotch commented Jun 9, 2016

Hello. Is there any "legal" way to replace route component from within onEnter() hook? I'd like to show, for example, a 404 page without redirect, but keeping the same url.
Now it's possible to render 404 from route component, but it requires checking for 404 conditions in every component for every route.

Something like:

onEnter: function(nextState, replace, callback) {   
    if(someCondition) {
       callback(<Error404 />);
    } else {
       callback();
    }
}

Now I'm using:

onEnter: function(nextState, replace, callback) {    
    nextState.routes.forEach(function(route, idx) {
        if(route.path === this.path) {
            route.component = <Error404 />;
            callback();
        }
    })
}

but it looks hackish. Thanks!

@timdorr
Copy link
Member

timdorr commented Jun 9, 2016

A better way would be to use getComponent and some global state store to communicate which component that should return. And for 404-ish pages, just having a wildcard catch-all route is normally the easiest way to do this.

@timdorr timdorr closed this as completed Jun 9, 2016
@taion
Copy link
Contributor

taion commented Jun 9, 2016

See also #3098.

@iotch
Copy link
Author

iotch commented Jun 9, 2016

Got it, thanks!

@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
None yet
Projects
None yet
Development

No branches or pull requests

3 participants