Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
36189a1
Added `React.ComponentProps<typeof Component>
piotrwitek Jan 24, 2019
533ea05
Added missing quote
piotrwitek Jan 24, 2019
92c9a5c
Add the example for useContext (#127)
sosukesuzuki Jan 26, 2019
38ccf55
Add useState example (#128)
sosukesuzuki Jan 26, 2019
446654f
Add examples for Context API (#129)
sosukesuzuki Jan 26, 2019
cbe31d9
Adds imports in module decleration
arshadkazmi42 Jan 30, 2019
fa0d274
README_SOURCE update and README generated using script
arshadkazmi42 Jan 30, 2019
23764a8
Added `React.ComponentProps<typeof Component>
piotrwitek Jan 24, 2019
d0e76f6
Added missing quote
piotrwitek Jan 24, 2019
bb8c555
Adds imports in module decleration
arshadkazmi42 Jan 30, 2019
b32ad8b
README_SOURCE update and README generated using script
arshadkazmi42 Jan 30, 2019
e592043
Merge branch 'master' into import-decleration
arshadkazmi42 Feb 3, 2019
dae1c72
Merge branch 'import-decleration' of github.com:arshadkazmi42/react-r…
arshadkazmi42 Feb 3, 2019
e602f90
Added `React.ComponentProps<typeof Component>
piotrwitek Jan 24, 2019
69756f2
Added missing quote
piotrwitek Jan 24, 2019
4da6157
Adds imports in module decleration
arshadkazmi42 Jan 30, 2019
b12c89f
README_SOURCE update and README generated using script
arshadkazmi42 Jan 30, 2019
a3facd0
Merge branch 'import-decleration' of github.com:arshadkazmi42/react-r…
arshadkazmi42 Feb 3, 2019
bdec1e5
Updated tests to fix build Closed #95 (#133)
piotrwitek Feb 3, 2019
8ccc7c8
Adds imports in module decleration
arshadkazmi42 Jan 30, 2019
9ee8c3f
README_SOURCE update and README generated using script
arshadkazmi42 Jan 30, 2019
e32c653
Added `React.ComponentProps<typeof Component>
piotrwitek Jan 24, 2019
05fd17f
Added missing quote
piotrwitek Jan 24, 2019
943e259
Merge branch 'import-decleration' of github.com:arshadkazmi42/react-r…
arshadkazmi42 Feb 4, 2019
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
Prev Previous commit
Next Next commit
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`.
  • Loading branch information
sosukesuzuki authored and piotrwitek committed Jan 26, 2019
commit 92c9a5ce03ad852e8421ddd55a4bca42375d5e45
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';

type Props = {};

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

type Props = {};

export default function ToggleThemeButton(props: Props) {
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;
33 changes: 4 additions & 29 deletions playground/src/context/theme-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
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> {
export class ThemeProvider extends React.Component<{}, State> {
readonly state: State = { theme: themes.light };

toggleTheme = () => {
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';

type Props = {};

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