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

CacheRoute inside another Function Component #55

Closed
chitgoks opened this issue Nov 6, 2019 · 2 comments
Closed

CacheRoute inside another Function Component #55

chitgoks opened this issue Nov 6, 2019 · 2 comments

Comments

@chitgoks
Copy link

chitgoks commented Nov 6, 2019

I commend this library because it saves a lot of new users from having to be forced to learn redux just to cater to this router issue.

My issue is because I created a functional component called ProtectedRoute where it returns the CacheRoute or a redirect if the user is not authenticated.

I place the ProtectedRoute tag in the CacheSwitch but it does not work. any idea what could be wrong with this code? Thank you

const ProtectedRoute = (props) => {
    return localStorage.getItem('user') !== null
        ? <CacheRoute {...props} />
        : <Redirect to="/login"/>;
};

export default ProtectedRoute;

And this is the cache switch

<CacheSwitch>
              <ProtectedRoute component={MyComponent} path="/" exact/>
</CacheSwitch>
@CJY0208
Copy link
Owner

CJY0208 commented Nov 6, 2019

Hi~

Nothing wrong with your code, just because CacheSwitch only saves the first layer of nodes which type is CacheRoute by default.

Can config that behavior using which prop on CacheSwitch

For your code

<CacheSwitch which={element => element.type === ProtectedRoute}>
  <ProtectedRoute component={MyComponent} path="/" exact/>
</CacheSwitch>

which prop is a function that would receive a instance of React Component, return true/false to decide if CacheSwitch need to save it

This prop has not been mentioned in the documentation yet.

Sorry about that and thanks for using react-router-cache-route !

@CJY0208 CJY0208 closed this as completed Nov 6, 2019
@chitgoks
Copy link
Author

chitgoks commented Nov 6, 2019

thank you! this works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants