I have to create the styles based on a param, for that i have done as below.
const useStyles = (isLayoutReadOnly = false) =>
makeStyles((theme: Theme) => {
return {
x: {
margin: theme.spacing(0, 0, 1, 0),
marginLeft: !isLayoutReadOnly ? '-20px' : undefined
}
}
};
});
And i am trying to use it this way,
const { x } = useStyles(isLayoutReadOnly);
Which throws me the error
property x does not exist on type '(props?: any) => ClassNameMap<"x">'
How should i fix this issue? I have tried setting the custom return types and also ReturnType<typeof makeStyles>, how ever, it is unable to detect.
You are setting
useStylesto a function that callsmakeStyles:The example in the docs shows
useStylesbeing set tomakeStylesdirectly rather than to a function:You can achieve the same pattern with your code by using an Immediately Invoked Function Expression. By wrapping your function in
(() => {})parenthesis and then adding()parenthesis at the end, you can call your function immediately to make sureuseStylesgets the return value of your(isLayoutReadOnly) => ...function. Here's how that would look:The other option is to adjust how you are calling
useStyles. You can add an additional set of parenthesis to your call touseStylesto call the function that is returned to get the expected behavior: