Skip to content

Commit

Permalink
feat: create PermissionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
synzhang committed Feb 22, 2022
1 parent db09bd9 commit 2df9d6e
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createContext, useContext } from 'react'

const PermissionContext = createContext({})

export const PermissionProvider = ({
roles,
policies,
currentUser,
children,
}) => (
<PermissionContext.Provider value={{ roles, policies, currentUser }}>
{children}
</PermissionContext.Provider>
)

export const usePermission = () => {
const context = useContext(PermissionContext)

if (typeof context === 'undefined') {
throw new Error('useContext must be used within a Provider')
}

return context
}

0 comments on commit 2df9d6e

Please sign in to comment.