-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance useTheme to provide default theme without ThemeProvider #105
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,21 +2,15 @@ import React from 'react'; | |
|
||
type CreateCtx<A> = readonly [ | ||
() => A, | ||
React.ProviderExoticComponent<React.ProviderProps<A | undefined>>, | ||
React.ProviderExoticComponent<React.ProviderProps<A>>, | ||
]; | ||
|
||
// create context with no upfront defaultValue | ||
// without having to do undefined check all the time | ||
function createCtx<A>(): CreateCtx<A> { | ||
const ctx = React.createContext<A | undefined>(undefined); | ||
function createCtx<A>(defaultContext: A): CreateCtx<A> { | ||
const ctx = React.createContext<A>(defaultContext); | ||
|
||
function useCtx(): A { | ||
const c = React.useContext(ctx); | ||
|
||
if (!c) throw new Error('useCtx must be inside a Provider with a value'); | ||
|
||
return c; | ||
} | ||
const useCtx = (): A => React.useContext(ctx); | ||
Comment on lines
+10
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very clever~! However, I am worried that many devs will be curious that I hope there is more thing related to this to read about. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hyochan Ok. I will write about it soon. |
||
|
||
// make TypeScript infer a tuple, not an array of union types | ||
return [useCtx, ctx.Provider] as const; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may remove
prefix
Style
when it is grouped underStyles
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. I'll remove it in other PR. (because it is not related to this PR.)