Skip to content

Commit 2f45e52

Browse files
sosukesuzukipiotrwitek
authored andcommitted
Add examples for Context API (#129)
Add examples for Context API
1 parent 877f71f commit 2f45e52

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

Diff for: README.md

+93
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
5252
- [Render Props](#render-props) 🌟 __NEW__
5353
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
5454
- [Redux Connected Components](#redux-connected-components)
55+
- [Context](#context)
5556
- [Hooks](#hooks)
5657
- [Redux](#redux)
5758
- [Action Creators](#action-creators) 📝 __UPDATED__
@@ -766,6 +767,98 @@ export default () => (
766767
767768
[⇧ back to top](#table-of-contents)
768769
770+
## Context
771+
772+
> https://reactjs.org/docs/context.html
773+
774+
#### ThemeContext
775+
776+
```tsx
777+
import * as React from 'react';
778+
779+
export type Theme = React.CSSProperties;
780+
781+
type Themes = {
782+
dark: Theme;
783+
light: Theme;
784+
};
785+
786+
export const themes: Themes = {
787+
dark: {
788+
color: 'black',
789+
backgroundColor: 'white',
790+
},
791+
light: {
792+
color: 'white',
793+
backgroundColor: 'black',
794+
},
795+
};
796+
797+
export type ThemeContextProps = { theme: Theme; toggleTheme?: () => void };
798+
const ThemeContext = React.createContext<ThemeContextProps>({ theme: themes.light });
799+
800+
export default ThemeContext;
801+
802+
```
803+
804+
[⇧ back to top](#table-of-contents)
805+
806+
#### ThemeProvider
807+
808+
```tsx
809+
import React from 'react';
810+
import ThemeContext, { themes, Theme } from './theme-context';
811+
import ToggleThemeButton from './theme-consumer';
812+
813+
interface State {
814+
theme: Theme;
815+
}
816+
export class ThemeProvider extends React.Component<{}, State> {
817+
readonly state: State = { theme: themes.light };
818+
819+
toggleTheme = () => {
820+
this.setState(state => ({
821+
theme: state.theme === themes.light ? themes.dark : themes.light,
822+
}));
823+
}
824+
825+
render() {
826+
const { theme } = this.state;
827+
const { toggleTheme } = this;
828+
return (
829+
<ThemeContext.Provider value={{ theme, toggleTheme }}>
830+
<ToggleThemeButton />
831+
</ThemeContext.Provider>
832+
);
833+
}
834+
}
835+
836+
```
837+
838+
[⇧ back to top](#table-of-contents)
839+
840+
#### ThemeConsumer
841+
842+
```tsx
843+
import * as React from 'react';
844+
import ThemeContext from './theme-context';
845+
846+
type Props = {};
847+
848+
export default function ToggleThemeButton(props: Props) {
849+
return (
850+
<ThemeContext.Consumer>
851+
{({ theme, toggleTheme }) => <button style={theme} onClick={toggleTheme} {...props} />}
852+
</ThemeContext.Consumer>
853+
);
854+
}
855+
856+
```
857+
858+
[Implementation with Hooks](#--usecontext)
859+
860+
[⇧ back to top](#table-of-contents)
861+
769862
## Hooks
770863
771864
> https://reactjs.org/docs/hooks-intro.html

Diff for: README_SOURCE.md

+25
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
5252
- [Render Props](#render-props) 🌟 __NEW__
5353
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
5454
- [Redux Connected Components](#redux-connected-components)
55+
- [Context](#context)
5556
- [Hooks](#hooks)
5657
- [Redux](#redux)
5758
- [Action Creators](#action-creators) 📝 __UPDATED__
@@ -309,6 +310,30 @@ const mapDispatchToProps = (dispatch: Dispatch<ActionType>) => ({
309310
310311
[⇧ back to top](#table-of-contents)
311312
313+
## Context
314+
315+
> https://reactjs.org/docs/context.html
316+
317+
#### ThemeContext
318+
319+
::codeblock='playground/src/context/theme-context.ts'::
320+
321+
[⇧ back to top](#table-of-contents)
322+
323+
#### ThemeProvider
324+
325+
::codeblock='playground/src/context/theme-provider.tsx'::
326+
327+
[⇧ back to top](#table-of-contents)
328+
329+
#### ThemeConsumer
330+
331+
::codeblock='playground/src/context/theme-consumer.tsx'::
332+
333+
[Implementation with Hooks](#--usecontext)
334+
335+
[⇧ back to top](#table-of-contents)
336+
312337
## Hooks
313338
314339
> https://reactjs.org/docs/hooks-intro.html

0 commit comments

Comments
 (0)