Skip to content

Instantly share code, notes, and snippets.

@iamandrewluca
Last active January 25, 2019 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamandrewluca/d84fc75d2c51560e094b21593a49019e to your computer and use it in GitHub Desktop.
Save iamandrewluca/d84fc75d2c51560e094b21593a49019e to your computer and use it in GitHub Desktop.
react-router, composition vs inheritance
import React from 'react'
import { ProtectionConsumer } from './ProtectionContext'
import { Route } from 'react-router-dom'
// Inheritance
export class ProtectedRouteInherited extends Route {
render() {
return (
<ProtectionConsumer>
{({ protected }) => {
if (!protected) { return null }
return super.render()
}}
</ProtectionConsumer>
)
}
}
// vs Composition, does not work inside Switch
export class ProtectedRouteComposed extends React.Component {
render() {
return (
<ProtectionConsumer>
{({ protected }) => {
if (!protected) { return null }
return <Route {...this.props} />
}}
</ProtectionConsumer>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment