-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #851 from navikt/useIsDebug
Lager en egen useIsDebug så det blir lettere å ta i bruke denne funksjonen andre steder
- Loading branch information
Showing
4 changed files
with
65 additions
and
58 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { ReactElement, useEffect, useState } from "react"; | ||
|
||
export const IsDebugContext: React.Context<IsDebugContextValues> = React.createContext({} as IsDebugContextValues); | ||
|
||
export type IsDebugContextValues = { | ||
isDebug: boolean; | ||
}; | ||
|
||
interface IsDebugProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
export function IsDebugProvider({ children }: IsDebugProviderProps): ReactElement { | ||
const [isDebug, setIsDebug] = useState(false); | ||
|
||
useEffect(() => { | ||
try { | ||
const valueFromLocalStorage = localStorage.getItem("isDebug"); | ||
if (valueFromLocalStorage && valueFromLocalStorage === "true") { | ||
setIsDebug(true); | ||
} | ||
} catch (err) { | ||
// ignore | ||
} | ||
}, []); | ||
|
||
return ( | ||
<IsDebugContext.Provider | ||
// eslint-disable-next-line | ||
value={{ | ||
isDebug, | ||
}} | ||
> | ||
{children} | ||
</IsDebugContext.Provider> | ||
); | ||
} | ||
|
||
const useIsDebug = (): IsDebugContextValues => { | ||
const context = React.useContext(IsDebugContext); | ||
return context; | ||
}; | ||
|
||
export default useIsDebug; |
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
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
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