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 4 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';

interface ThemedButtonProps {}

export default function ThemeToggleButton(props: ThemedButtonProps) {
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';

interface ThemedButtonProps {}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to refactor all the interface ThemedButtonProps to just type Props =. I prefer that now as it is more concise and cleaner, also has a benefit to do things like this:

type Props = typeof dispatchProps & ReturnType<typeof mapStateToProps>;

Please could you do it for all the components related to this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I can:+1:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome, thanks!


export default function ToggleThemeButton(props: ThemedButtonProps) {
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;
31 changes: 3 additions & 28 deletions playground/src/context/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
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> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rename to ThemeProvider

readonly state: State = { theme: themes.light };
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';

interface ThemedButtonProps {}

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