Skip to content

Commit 4c670b5

Browse files
sosukesuzukipiotrwitek
authored andcommitted
Add the example for useContext (#127)
resolve #120 (comment) - divide `playground/src/context/theme-provider.tsx` to `theme-provider.tsx`, `theme-consumer.tsx` and `theme-context.ts`. - add the example for `useContext` to `playground/src/hooks/use-theme-context.tsx`.
1 parent a7baa43 commit 4c670b5

File tree

6 files changed

+84
-29
lines changed

6 files changed

+84
-29
lines changed

Diff for: README.md

+23
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,29 @@ export default Counter;
826826
827827
[⇧ back to top](#table-of-contents)
828828
829+
#### - useContext
830+
831+
> https://reactjs.org/docs/hooks-reference.html#usecontext
832+
833+
```tsx
834+
import * as React from 'react';
835+
import ThemeContext from '../context/theme-context';
836+
837+
type Props = {};
838+
839+
export default function ThemeToggleButton(props: Props) {
840+
const { theme, toggleTheme } = React.useContext(ThemeContext);
841+
return (
842+
<button onClick={toggleTheme} style={theme} >
843+
Toggle Theme
844+
</button>
845+
);
846+
}
847+
848+
```
849+
850+
[⇧ back to top](#table-of-contents)
851+
829852
---
830853
831854
# Redux

Diff for: README_SOURCE.md

+8
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ Hook for state management like Redux in a function component.
320320
321321
[⇧ back to top](#table-of-contents)
322322
323+
#### - useContext
324+
325+
> https://reactjs.org/docs/hooks-reference.html#usecontext
326+
327+
::codeblock='playground/src/hooks/use-theme-context.tsx'::
328+
329+
[⇧ back to top](#table-of-contents)
330+
323331
---
324332
325333
# Redux

Diff for: playground/src/context/theme-consumer.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as React from 'react';
2+
import ThemeContext from './theme-context';
3+
4+
type Props = {};
5+
6+
export default function ToggleThemeButton(props: Props) {
7+
return (
8+
<ThemeContext.Consumer>
9+
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />}
10+
</ThemeContext.Consumer>
11+
);
12+
}

Diff for: playground/src/context/theme-context.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
3+
export type Theme = React.CSSProperties;
4+
5+
type Themes = {
6+
dark: Theme;
7+
light: Theme;
8+
};
9+
10+
export const themes: Themes = {
11+
dark: {
12+
color: 'black',
13+
backgroundColor: 'white',
14+
},
15+
light: {
16+
color: 'white',
17+
backgroundColor: 'black',
18+
},
19+
};
20+
21+
export type ThemeContextProps = { theme: Theme; toggleTheme?: () => void };
22+
const ThemeContext = React.createContext<ThemeContextProps>({ theme: themes.light });
23+
24+
export default ThemeContext;

Diff for: playground/src/context/theme-provider.tsx

+4-29
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
import React from 'react';
2+
import ThemeContext, { themes, Theme } from './theme-context';
3+
import ToggleThemeButton from './theme-consumer';
24

3-
// Context
4-
const themes = {
5-
dark: {
6-
color: 'black',
7-
backgroundColor: 'white',
8-
} as React.CSSProperties,
9-
light: {
10-
color: 'white',
11-
backgroundColor: 'black',
12-
} as React.CSSProperties,
13-
};
14-
15-
type Theme = { theme: React.CSSProperties; toggleTheme?: () => void };
16-
const ThemeContext = React.createContext<Theme>({ theme: themes.light });
17-
18-
// Provider
195
interface State {
20-
theme: Theme['theme'];
6+
theme: Theme;
217
}
22-
export class App extends React.Component<{}, State> {
8+
export class ThemeProvider extends React.Component<{}, State> {
239
readonly state: State = { theme: themes.light };
2410

2511
toggleTheme = () => {
@@ -38,14 +24,3 @@ export class App extends React.Component<{}, State> {
3824
);
3925
}
4026
}
41-
42-
// Consumer
43-
interface ThemedButtonProps {}
44-
45-
function ToggleThemeButton(props: ThemedButtonProps) {
46-
return (
47-
<ThemeContext.Consumer>
48-
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />}
49-
</ThemeContext.Consumer>
50-
);
51-
}

Diff for: playground/src/hooks/use-theme-context.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from 'react';
2+
import ThemeContext from '../context/theme-context';
3+
4+
type Props = {};
5+
6+
export default function ThemeToggleButton(props: Props) {
7+
const { theme, toggleTheme } = React.useContext(ThemeContext);
8+
return (
9+
<button onClick={toggleTheme} style={theme} >
10+
Toggle Theme
11+
</button>
12+
);
13+
}

0 commit comments

Comments
 (0)