diff --git a/README.md b/README.md index 173ab24..e5986b8 100644 --- a/README.md +++ b/README.md @@ -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 ( + + ); +} + +``` + +[⇧ back to top](#table-of-contents) + --- # Redux diff --git a/README_SOURCE.md b/README_SOURCE.md index fa2a059..47476fe 100644 --- a/README_SOURCE.md +++ b/README_SOURCE.md @@ -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 diff --git a/playground/src/context/theme-consumer.tsx b/playground/src/context/theme-consumer.tsx new file mode 100644 index 0000000..5ddd73d --- /dev/null +++ b/playground/src/context/theme-consumer.tsx @@ -0,0 +1,12 @@ +import * as React from 'react'; +import ThemeContext from './theme-context'; + +type Props = {}; + +export default function ToggleThemeButton(props: Props) { + return ( + + {({ theme, toggleTheme }) => + ); +}