Created
March 11, 2019 15:48
-
-
Save oscarr-reyes/b965d5c13a095866931b50a4334b8711 to your computer and use it in GitHub Desktop.
Flow throws error for rendering Component passed from a prop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @flow | |
import React, { Component } from "react"; | |
import { Redirect, Route } from "react-router-dom"; | |
import CookieStorage from "./../services/CookieStorage"; | |
import type { Component as ComponentType } from "react"; | |
type Props = { | |
component: ComponentType<any, any> | |
} | |
class ProtectedRoute extends Component<Props> { | |
render() { | |
const isAuthenticated = this.isAuthenticated(); | |
const {...props} = this.props; | |
const AuthorizedComponent = this.props.component; | |
return ( | |
<Route | |
{...props} | |
render={props => ( | |
isAuthenticated ? | |
<AuthorizedComponent {...props} /> : | |
<Redirect to="/"/> | |
)} | |
/> | |
); | |
} | |
isAuthenticated(): boolean { | |
const data = CookieStorage.get("foobar"); | |
return data !== null; | |
} | |
} | |
export default ProtectedRoute; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Flow throws: