Skip to content

Add examples for Context API #129

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 8 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
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
- [Render Props](#render-props) 🌟 __NEW__
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
- [Redux Connected Components](#redux-connected-components)
- [Context](#context)
- [Hooks](#hooks)
- [Redux](#redux)
- [Action Creators](#action-creators) 📝 __UPDATED__
Expand Down Expand Up @@ -766,6 +767,98 @@ export default () => (

[⇧ back to top](#table-of-contents)

## Context

> https://reactjs.org/docs/context.html

#### ThemeContext

```tsx
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;

```

[⇧ back to top](#table-of-contents)

#### ThemeProvider

```tsx
import React from 'react';
import ThemeContext, { themes, Theme } from './theme-context';
import ToggleThemeButton from './theme-consumer';

interface State {
theme: Theme;
}
export class ThemeProvider extends React.Component<{}, State> {
readonly state: State = { theme: themes.light };

toggleTheme = () => {
this.setState(state => ({
theme: state.theme === themes.light ? themes.dark : themes.light,
}));
}

render() {
const { theme } = this.state;
const { toggleTheme } = this;
return (
<ThemeContext.Provider value={{ theme, toggleTheme }}>
<ToggleThemeButton />
</ThemeContext.Provider>
);
}
}

```

[⇧ back to top](#table-of-contents)

#### ThemeConsumer

```tsx
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>
);
}

```

[Implementation with Hooks](#--usecontext)

[⇧ back to top](#table-of-contents)

## Hooks

> https://reactjs.org/docs/hooks-intro.html
Expand Down
25 changes: 25 additions & 0 deletions README_SOURCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This gives you the power to prioritize our work and support the project contribu
- [Render Props](#render-props) 🌟 __NEW__
- [Higher-Order Components](#higher-order-components) 📝 __UPDATED__
- [Redux Connected Components](#redux-connected-components)
- [Context](#context)
- [Hooks](#hooks)
- [Redux](#redux)
- [Action Creators](#action-creators) 📝 __UPDATED__
Expand Down Expand Up @@ -309,6 +310,30 @@ const mapDispatchToProps = (dispatch: Dispatch<ActionType>) => ({

[⇧ back to top](#table-of-contents)

## Context

> https://reactjs.org/docs/context.html

#### ThemeContext

::codeblock='playground/src/context/theme-context.ts'::

[⇧ back to top](#table-of-contents)

#### ThemeProvider

::codeblock='playground/src/context/theme-provider.tsx'::

[⇧ back to top](#table-of-contents)

#### ThemeConsumer

::codeblock='playground/src/context/theme-consumer.tsx'::

[Implementation with Hooks](#--usecontext)

[⇧ back to top](#table-of-contents)

## Hooks

> https://reactjs.org/docs/hooks-intro.html
Expand Down