Closed thehig closed 2 years ago
Hi @thehig,
I'm assuming you are using TSS in a JavaScript project, can you confirm?
I assume you might be using TSS incorrectly somewhere.
Did you use the codemod to migrate to TSS or did you do it by hand?
Maybe you are calling makeStyles
this way:
const useStyles = makeStyles(theme => ({
root: {}
});
instead of this way: (which is the correct way)
const useStyles = makeStyles()(theme => ({
root: {}
});
I'm assuming you are using TSS in a JavaScript project, can you confirm?
Yep
Did you use the codemod to migrate to TSS or did you do it by hand?
I did use the codemod, yes, but it didn't hit everything.
I just checked my code and I only use the makeStyles in 4 places, always with the "correct" structure from the post above.
I went through my codebase and removed all usages of the makeStyles
function but the error persists. Could the same kind of structure/invocation bug come from withStyles
usage?
Yes, withStyles
uses makeStyles
internally.
Maybe rename your .js
files into .tsx
files just to let TypeScript tell you what's wrong.
Maybe rename your
.js
files into.tsx
files just to let TypeScript tell you what's wrong.
Thats not the easiest solution in such a large codebase. The thing that bugs me is I don't set direction
anywhere so it must be coming from the base theme
Not everywhere, just on some files, momentarily to see if typescript can detect an error.
Do you use the theme <ThemeProvider />
as described in the doc? https://docs.tss-react.dev/readme-1
If I could see the code I would probably be able to spot the error quickly
Just in cas, It may help, https://docs.tss-react.dev/page-1/withstyles
Do you use the theme
as described in the doc? https://docs.tss-react.dev/readme-1
Yep
So, I create my Theme based on some theme
object in state, some theme
object located in my config, and createTheme
func.
import { createSelector } from "reselect";
import { createTheme } from "@mui/material/styles";
import config from "config/index";
// Get the tenant theme configuration and *MERGE WITH* config if present
const selectTheme = createSelector(
(state) => selectTenant(state).theme,
(theme = {}) => {
// Make sure that the theme we're initializing is not the same reference in state as the tenant response
const tenantTheme = JSON.parse(JSON.stringify(theme));
console.log("Tenant Theme", tenantTheme);
let themes = tenantTheme;
if (config.has("theme")) {
const configTheme = config.get("theme");
console.log("Config Theme", configTheme);
themes = merge(tenantTheme, configTheme);
}
const newTheme = createTheme(themes);
console.log("Completed Theme", newTheme);
return newTheme;
}
);
And then the component
import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { ThemeProvider } from "@mui/material/styles";
import createCache from "@emotion/cache";
import { CacheProvider } from "@emotion/react";
export const muiCache = createCache({
key: "mui",
prepend: true,
});
class TenantProviders extends PureComponent {
//... some other code stuff
render = () => {
const {
onError,
props: { theme, children },
} = this;
// Wrapped in some other providers for INTL and Localization, ommitted for clarity
return (
<CacheProvider value={muiCache}>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</CacheProvider>
);
};
}
// Redux mapStateToProps invokes the `selectTheme` selector above
I tried to make a sample to demonstrate the issue I'm having, but it doesn't have the same error.
https://codesandbox.io/s/bold-bird-v2t5pc?file=/src/Component.jsx
I think I figured it out, I had a botched attempt at replicating the withTheme
behavior from older MUI. Any tips on how to implement this?
Glad you figured it out.
This would be withTheme
:
import { useStyles } from "tss-react/mui";
function withTheme(Component){
function ComponentWithTheme(props){
const { theme }= useStyles();
return <Component {...props} theme={theme} />;
}
ComponentWithTheme.name = `${Component.name}WithTheme`;
return ComponentWithTheme;
}
Yup that worked! Thanks, and sorry for the bother
No problem, happy to help
I've recently switched from
@mui/styles
to react-tss and I'm experiencing an error:Uncaught TypeError: can't assign to property "label" on "ltr": not an object
Tracking the error down I'm lead to the makeStyles.js file, line ~45
In this error case, the
ruleName
isdirection
resulting in acssObject
of the stringltr
. Attempting to write to cssObject.label causes the error above.I've checked my codebase and I'm not modifying the direction anywhere, so I don't really understand why this is happening and could really use some assistance.
Thanks