Skip to content

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
merged 7 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,29 @@ export default Counter;
[⇧ back to top](#table-of-contents)
#### - useContext
> https://reactjs.org/docs/hooks-reference.html#usecontext
```tsx
import * as React from 'react';
import ThemeContext from '../context/theme-context';

type Props = {};

export default function ThemeToggleButton(props: Props) {
const { theme, toggleTheme } = React.useContext(ThemeContext);
return (
<button onClick={toggleTheme} style={theme} >
Toggle Theme
</button>
);
}

```
[⇧ back to top](#table-of-contents)
---
# Redux
Expand Down
8 changes: 8 additions & 0 deletions README_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,14 @@ Hook for state management like Redux in a function component.
[⇧ back to top](#table-of-contents)
#### - useContext
> https://reactjs.org/docs/hooks-reference.html#usecontext
::codeblock='playground/src/hooks/use-theme-context.tsx'::
[⇧ back to top](#table-of-contents)
---
# Redux
Expand Down
12 changes: 12 additions & 0 deletions playground/src/context/theme-consumer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';
import ThemeContext from './theme-context';

type Props = {};

export default function ToggleThemeButton(props: Props) {
return (
<ThemeContext.Consumer>
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />}
</ThemeContext.Consumer>
);
}
24 changes: 24 additions & 0 deletions playground/src/context/theme-context.ts
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;
33 changes: 4 additions & 29 deletions playground/src/context/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
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> {
export class ThemeProvider extends React.Component<{}, State> {
readonly state: State = { theme: themes.light };

toggleTheme = () => {
Expand All @@ -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>
);
}
13 changes: 13 additions & 0 deletions playground/src/hooks/use-theme-context.tsx
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';

type Props = {};

export default function ThemeToggleButton(props: Props) {
const { theme, toggleTheme } = React.useContext(ThemeContext);
return (
<button onClick={toggleTheme} style={theme} >
Toggle Theme
</button>
);
}