Skip to content

Instantly share code, notes, and snippets.

@cqfd
Last active January 11, 2017 03:41
Show Gist options
  • Save cqfd/642f307097b6f174b2c85d1dba8c2f58 to your computer and use it in GitHub Desktop.
Save cqfd/642f307097b6f174b2c85d1dba8c2f58 to your computer and use it in GitHub Desktop.
Admin-only React components with Flow
// @flow
import React from 'react'
import type { AdminProof } from './User'
export default function AdminOnly(props: { proof: AdminProof }) {
return <p>Secret admin stuff!</p>
}
// @flow
import React from 'react'
import AdminOnly from './AdminOnly'
import { checkIfAdmin } from './User'
function App() {
const currentUser = ...
const proof = checkIfAdmin(currentUser)
if (proof != null) {
return <AdminOnly proof={proof} />
} else {
return <p>Nothing to see here...</p>
}
}
// @flow
class _AdminProof {}
export type AdminProof = _AdminProof
export function checkIfAdmin(user: { isAdmin: bool }): ?AdminProof {
if (user.isAdmin) {
return new _AdminProof
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment