-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add the example for useContext #127
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
Merged
piotrwitek
merged 7 commits into
piotrwitek:master
from
sosukesuzuki:add-use-context-example
Jan 26, 2019
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8700bbb
Modify to divide theme-provider.tsx
sosukesuzuki 71f4e94
Add the example for useContext hook
sosukesuzuki 63a8362
Modify to update README_SOURCE
sosukesuzuki 3a7ddcd
Modify to generate new README.md
sosukesuzuki 76c34ad
Modify to rename App => ThemeProvider
sosukesuzuki b4a29bb
Modify to use 'type' instead of 'interface' when we declare Props typ…
sosukesuzuki f871c41
Modify to generate new README.md
sosukesuzuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as React from 'react'; | ||
import ThemeContext from './theme-context'; | ||
|
||
interface ThemedButtonProps {} | ||
|
||
export default function ToggleThemeButton(props: ThemedButtonProps) { | ||
return ( | ||
<ThemeContext.Consumer> | ||
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />} | ||
</ThemeContext.Consumer> | ||
); | ||
} |
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,24 @@ | ||
import * as React from 'react'; | ||
|
||
export type Theme = React.CSSProperties; | ||
|
||
type Themes = { | ||
dark: Theme; | ||
light: Theme; | ||
}; | ||
|
||
export const themes: Themes = { | ||
dark: { | ||
color: 'black', | ||
backgroundColor: 'white', | ||
}, | ||
light: { | ||
color: 'white', | ||
backgroundColor: 'black', | ||
}, | ||
}; | ||
|
||
export type ThemeContextProps = { theme: Theme; toggleTheme?: () => void }; | ||
const ThemeContext = React.createContext<ThemeContextProps>({ theme: themes.light }); | ||
|
||
export default ThemeContext; |
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 |
---|---|---|
@@ -1,23 +1,9 @@ | ||
import React from 'react'; | ||
import ThemeContext, { themes, Theme } from './theme-context'; | ||
import ToggleThemeButton from './theme-consumer'; | ||
|
||
// Context | ||
const themes = { | ||
dark: { | ||
color: 'black', | ||
backgroundColor: 'white', | ||
} as React.CSSProperties, | ||
light: { | ||
color: 'white', | ||
backgroundColor: 'black', | ||
} as React.CSSProperties, | ||
}; | ||
|
||
type Theme = { theme: React.CSSProperties; toggleTheme?: () => void }; | ||
const ThemeContext = React.createContext<Theme>({ theme: themes.light }); | ||
|
||
// Provider | ||
interface State { | ||
theme: Theme['theme']; | ||
theme: Theme; | ||
} | ||
export class App extends React.Component<{}, State> { | ||
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. Please rename to ThemeProvider |
||
readonly state: State = { theme: themes.light }; | ||
|
@@ -38,14 +24,3 @@ export class App extends React.Component<{}, State> { | |
); | ||
} | ||
} | ||
|
||
// Consumer | ||
interface ThemedButtonProps {} | ||
|
||
function ToggleThemeButton(props: ThemedButtonProps) { | ||
return ( | ||
<ThemeContext.Consumer> | ||
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />} | ||
</ThemeContext.Consumer> | ||
); | ||
} |
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,13 @@ | ||
import * as React from 'react'; | ||
import ThemeContext from '../context/theme-context'; | ||
|
||
interface ThemedButtonProps {} | ||
|
||
export default function ThemeToggleButton(props: ThemedButtonProps) { | ||
const { theme, toggleTheme } = React.useContext(ThemeContext); | ||
return ( | ||
<button onClick={toggleTheme} style={theme} > | ||
Toggle Theme | ||
</button> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would like to refactor all the
interface ThemedButtonProps
to justtype Props =
. I prefer that now as it is more concise and cleaner, also has a benefit to do things like this:Please could you do it for all the components related to this PR?
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.
Looks good! I can:+1:
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.
That's awesome, thanks!