-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
makeStyles vs withStyles (with Example) without react refs #15351
Comments
@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 👍. |
Could you elaborate what you mean by that? |
thanks olivier, @eps1lon i just meant comparing the use of const Avatar = React.forwardRef(function Avatar(props, ref) { ..
<div and things/>
}
export default withStyles(styles, {name: 'MuiAvatar'})(Avatar) vs function Avatatar(props){
const classes = useAvatarStyles()
<div and things/>
..} with makeStyles/hooks you don't need to wrap the component, meaning you don't need to worry about ref forwarding etc (which should make everything muuuch simpler) as you can see in the picture i posted, the class name is still MuiAvatar without using a forward ref, just passing in the name option to makeStyles in the same was you do with withStyles there is a huge benefit of doing this as well, in that you are really no longer coupled to react. the 'makeStyles' implementation is now basically a stylesheet which is framework agnostic |
Ref forwarding is not required for for withStyles or makeStyles. Your first example has the additional benefit that you can attach a ref to the |
Of course, but the second version doesnt use an hoc, so is forward ref even neccessary? |
I don't follow. The second example doesn't even use the forwardRef. I don't know if you need to forward the ref. That depends on your use case. |
It's a copy, literally copy and pasted copy, of the Avatar component from this library, not using an HOC and not using forwardRef, the picture is showing the MuiAvatar class still applies without using withStyles at all. So, if you need to create a downstream ref, the user can just create it now, because there are no wrappers. What im asking is.. -Hoc equivalent to: |
@jeremy-coleman I'm sorry. I don't understand your point regarding the ref. You have to use forwardRef anyway. The usage of makeStyles or withStyles or styled-components or emotion doesn't change a thing. |
Anyway, I believe we have answered your question. There is no reason not to do it. We are investigating a migration to this pattern in #15023. |
Ahh ok , your take is just always wrap with a ref because there’s no reason not to? thanks Olivier. I rewrote pretty much the whole library using make styles, i can send a copy if you want. Its in typescript and no prop types though |
@jeremy-coleman Oh wow, what was your motivation for the large enterprise? :) |
We need the ref internally and many of our users need it as well. We recommended the We want to switch to |
@oliver bc things kept breaking, wrong types, themes not applying , multiple versions of jss, etc etc. i also wanted to take out prop types for dev. Prod build is much smaller, 500kb pre 70k post minify |
@jeremy-coleman Do you think that some improvement could be bring back in the core of Material-UI? |
@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.
This could happen in earlier alphas if you didn't follow our installation instructions for
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 |
hello i'm new in material ui so can someone explain to me how css work by using withStyle and what a different between withStyle and MakeStyle.. |
going 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
The text was updated successfully, but these errors were encountered: