makeStyles vs withStyles (with Example) without react refs
See original GitHub issuegoing forward is makeStyles what you’re looking to do?
seems a lot easier than all the ref spam? is there any reason NOT to do this ? asking as both from a material-ui perspective and authoring components externally
I randomly picked the Avatar as an example and just put it inside one of the example dashboard apps… attached a pic of devtools passing mui name correctly
import React from 'react';
import clsx from 'clsx';
import {withStyles, makeStyles} from '@material-ui/styles';
export const useAvatarCss = makeStyles(theme => ({
/* Styles applied to the root element. */
root: {
position: 'relative',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
flexShrink: 0,
width: 40,
height: 40,
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.pxToRem(20),
borderRadius: '50%',
overflow: 'hidden',
userSelect: 'none',
},
/* Styles applied to the root element if there are children and not `src` or `srcSet`. */
colorDefault: {
color: theme.palette.background.default,
backgroundColor:
theme.palette.type === 'light' ? theme.palette.grey[400] : theme.palette.grey[600],
},
/* Styles applied to the img element if either `src` or `srcSet` is defined. */
img: {
width: '100%',
height: '100%',
textAlign: 'center',
// Handle non-square image. The property isn't supported by IE 11.
objectFit: 'cover',
},
system: {}
}), { name: 'MuiAvatar' })
type AvatarBaseProps = {
alt?: string;
childrenClassName?: string;
imgProps?: React.HtmlHTMLAttributes<HTMLImageElement>;
sizes?: string;
src?: string;
srcSet?: string;
component?: any
} & React.HTMLProps<any>
export function Avatar(props: AvatarBaseProps) {
const {
alt,
children: childrenProp,
childrenClassName: childrenClassNameProp,
//classes,
className: classNameProp,
component: Component,
imgProps,
sizes,
src,
srcSet,
...other
} = props;
const classes = useAvatarCss()
let children = null;
const img = src || srcSet;
if (img) {
children = (
<img
alt={alt}
src={src}
srcSet={srcSet}
sizes={sizes}
className={classes.img}
{...imgProps}
/>
);
} else if (childrenClassNameProp && React.isValidElement<any>(childrenProp)) {
children = React.cloneElement<any, any>((childrenProp as any), {
className: clsx(childrenClassNameProp, childrenProp.props.className),
});
} else {
children = childrenProp;
}
return (
<Component
className={clsx(classes.root, classes.system, {
[classes.colorDefault]: !img,
}, classNameProp)}
//ref={ref}
{...other}
>
{children}
</Component>
);
}
Avatar.defaultProps = {
name: 'MuiAvatar',
component: 'div',
};
export default Avatar
Issue Analytics
- State:
- Created 4 years ago
- Comments:17 (10 by maintainers)
Top Results From Across the Web
makeStyles vs withStyles (with Example) without react refs
I randomly picked the Avatar as an example and just put it inside one of the example dashboard apps.. attached a pic of...
Read more >What is the benefit of using withStyles over makeStyles?
They both provide the same functionality and there is no difference in the styles parameter for withStyles and makeStyles .
Read more >Styles API - MUI System
createStyles(styles) => styles This function doesn't really "do anything" at runtime, it's just the identity function. Its only purpose is to defeat TypeScript ......
Read more >What is the benefit of using withStyles over makeStyles?-Reactjs
They both provide the same functionality and there is no difference in the styles parameter for withStyles and makeStyles .
Read more >Material-UI makeStyles, useStyles, createStyles, and ...
Preferably, you are using React hooks and the MUI makeStyles hook (more on that below). ... Below is an example of using withStyles...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@jeremy-coleman makeStyles and withStyles have their own use cases. I would use the API that reduces boilerplate. In doubt, I would use makeStyles over withStyles. The main use case of withStyles is for building custom variations of our components. To answer your question, yes, it’s fine 👍.
@jeremy-coleman It seems like you’re pretty frustrated which results in you making statements that aren’t very constructive. I understand that this is a rant but please keep it civil.
That being sad let’s try to unpack your statement:
I would like to help you but I need specifics for those: What did you do? What did you expect? What happened instead?
Types in the core are not coupled with the implementation.
withStylesormakeStylesusage in the implementation does not change our types.withStylesis just a hoc wrapper aroundmakeStyles. It seems unlikely that the usage causes this breakage.This could happen in earlier alphas if you didn’t follow our installation instructions for
@material-ui/styles. SincewithStylesusesmakeStylesnow there shouldn’t be duplicate versions of jss anymore.This is self-evident: If you remove features your bundle will be smaller. Also 500k is probably post minify and 70k post compression. 70k before compression doesn’t sound like it’s bundling the full
@material-ui/corebundle.