-
I'm having a heck of a time getting my typings correct so I was curious if anyone had a working example or some feedback as to where I am going wrong. There are two things I need to happen with this instance of I took the example from the documentation website, and of course right off the bat its already throwing TypeScript errors, which is understandable: (pasting screenshot just to show typescript errors) The error found here (Error 1) is: button: {
background: ({ theme }) => theme.colorPrimary
},
Makes sense to an extent - it doesn't know what our theme consists of, so moving on... The error found here (Error 2) for const classes = useStyles({ ...props, theme });
Right away.. I'm a little confused. But let's type some things and see how it works out... Adding an interface for my theme doesn't seem to change any of the above errors: interface PUTheme {
colorPrimary: string;
altColor: string;
}
const theme: PUTheme = {
colorPrimary: "green",
altColor: "red"
}; So since adding an interface to my theme object doesn't seem to accomplish much, I was able to satisfy the error from Error 1 by adding typings. While this calms TypeScript down it feels, wrong? const useStyles = createUseStyles({
button: {
background: ({ theme }: {theme: PUTheme}) => theme.colorPrimary
},
label: {
fontWeight: "bold"
}
}); However, this still leaves me with Error 2 except the error has changed slightly
I've tried following back the typings in Does anyone have any tips or insight into what I'm doing wrong or working examples? Also, here is a link to my code sandbox if you wish to see it in action. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think I've found the solution. The passing of theme/props to the classes still feels strange to me. Can anyone verify this is the correct way to do it? Thanks so much! import React, { ReactNode } from "react";
import { createUseStyles, useTheme, ThemeProvider } from "react-jss";
interface Props {
children: ReactNode;
useRed: boolean;
}
interface PUTheme {
colorPrimary: string;
altColor: string;
}
const useStyles = createUseStyles({
button: {
background: ({
theme,
useRed
}: {
theme: PUTheme;
useRed: Props["useRed"];
}) => (useRed ? theme.altColor : theme.colorPrimary)
},
label: {
fontWeight: "bold"
}
});
const Button2: React.FC<Props> = ({ children, ...props }) => {
const theme = useTheme<PUTheme>();
const classes = useStyles({ ...props, theme });
return (
<button className={classes.button}>
<span className={classes.label}>{children}</span>
</button>
);
};
const theme: PUTheme = {
colorPrimary: "green",
altColor: "red"
};
const App = () => (
<ThemeProvider theme={theme}>
<Button2 useRed={true}>I am a button 2 with green background</Button2>
</ThemeProvider>
);
export default App; View working Code Sandbox here! |
Beta Was this translation helpful? Give feedback.
I think I've found the solution. The passing of theme/props to the classes still feels strange to me. Can anyone verify this is the correct way to do it? Thanks so much!