React router private routes / redirect not working

后端 未结 9 1272
轮回少年
轮回少年 2020-12-07 16:57

I have slightly adjusted the React Router example for the private routes to play nice with Redux, but no components are rendered when Linking or Redirecting to other \'pages

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 17:24

    Typescript

    If you are looking for a solution for Typescript, then I made it work this way,

    const PrivateRoute = ({ component: Component, ...rest }: any) => (
        
                localStorage.getItem("authToken") ? (
                    
                ) : (
                        
                    )
            }
        />
    );
    
    
        
            
            
        
    
    

    Just in case you want to go by creating a class then something like this,

    class PrivateRoute extends Route {
        render() {
            if (localStorage.getItem("authToken")) {
                return 
            } else {
                return 
            }
        }
    }
    

提交回复
热议问题