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
Add withTheme definition #521
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as React from "react"; | ||
|
||
import styled from ".."; | ||
import { css, keyframes, ThemeProvider, injectGlobal, withTheme, ThemeProps } from ".."; | ||
|
||
|
||
class MyComponent extends React.Component<ThemeProps<{}>, {}> { | ||
render() { | ||
const { theme } = this.props; | ||
|
||
console.log("Current theme: ", theme); | ||
|
||
return <h1>Hello</h1>; | ||
} | ||
} | ||
|
||
export default withTheme(MyComponent); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import * as React from "react"; | ||
import { StatelessComponent, ComponentClass } from "react"; | ||
import { StatelessComponent, ComponentClass, PureComponent } from "react"; | ||
|
||
type Component<P> = ComponentClass<P> | StatelessComponent<P>; | ||
type Component<P> = ComponentClass<P> | StatelessComponent<P> | PureComponent<P, any>; | ||
|
||
export interface ThemeProps<T> { | ||
theme: T; | ||
|
@@ -199,6 +199,8 @@ export interface ThemedStyledComponentsModule<T> { | |
declare const styled: StyledInterface; | ||
|
||
export const css: ThemedCssFunction<any>; | ||
export function withTheme<C extends Component<any>>(component: C): C; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
class MyComponent extends React.Component<ThemeProps<{}>, {}> {
render() {
const { theme } = this.props;
console.log("Current theme: ", theme);
return <h1>Hello</h1>;
}
}
export default withTheme(MyComponent);
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd suggest something like this here: export function withTheme<P extends { theme?: T; }, T>(component: Component<P>): ComponentClass<rest(P, 'theme')>; where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would you need to remove the property? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An inner class is required to have interface MyComponentProps {
theme: MyTheme; // I want have this required
text: string;
}
class MyComponent extends React.Component<MyComponentProps, {}> {
...
}
<MyComponentProps theme={theme} text={text} /> // ok
<MyComponentProps text={text} /> // error, I can't use without theme so after applying the interface MyThemedComponentProps {
text: string;
}
const MyThemedComponent: ComponentClass<MyThemedComponentProps> = withTheme(MyComponent);
<MyThemedComponent text={text} /> // now that is ok
<MyThemedComponent theme={theme} text={text} /> // and now that is an error, because there's no theme prop anymore |
||
|
||
export function keyframes(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): string; | ||
export function injectGlobal(strings: TemplateStringsArray, ...interpolations: SimpleInterpolation[]): void; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
PureComponent
should be here.PureComponent
is an instance, but here must be a constructor.What the use case required this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw how withRouter was implemented in React Router, they were also using PureComponent. I'll remove this :)