Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Modify to divide theme-provider.tsx
  • Loading branch information
sosukesuzuki committed Jan 26, 2019
commit 8700bbbd8fb0ce7c264975faad4d5710fc8b12ff
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>
);
}