简体   繁体   English

MUI v5 @mui/material/styles vs @emotion/react

[英]MUI v5 @mui/material/styles vs @emotion/react

This is a follow-up question, to this question about theming MUI v5.这是一个后续问题,到这个关于主题化MUI V5问题。 Although my questions were answered, I'm still having difficulties understanding where exactly the @emotion/react API and when the @mui/material/styles should be used.尽管我的问题得到了解答,但我仍然难以理解@emotion/react API 的确切位置以及何时应该使用@mui/material/styles I read to the migration guide , while this does answer some 'how to' questions, I don't understand the bigger picture.我阅读了迁移指南,虽然这确实回答了一些“如何”问题,但我不了解更大的图景。

When do I use MUI's styling, when emotion's?我什么时候用 MUI 的样式,什么时候用情感? Why are there two styling solutions?为什么有两种样式解决方案? Should I use MUI's ThemeProvider or Emotion's?我应该使用 MUI 的ThemeProvider还是 Emotion 的? What are the differences?有什么区别? These questions might be trivial to an experienced user, but as someone who's just getting started this is all a lot to take in and understand.这些问题对于有经验的用户来说可能是微不足道的,但作为一个刚刚入门的人,这一切都需要接受和理解。

MUI v5 uses emotion as a default style engine. MUI v5使用情感作为默认样式引擎。 The MUI team recommends styled / sx to customize the component styles. MUI 团队推荐styled / sx来自定义组件样式。 Under the hood, it calls the styled() function from emotion (not to be confused with MUI styled ), that's where the emotion part relevant.在幕后,它从情感中调用styled()函数(不要与 MUI styled混淆),这就是情感部分相关的地方。 You don't need to care about the ThemeProvider from emotion if you are using MUI.如果您使用 MUI,则无需从情感上​​关心ThemeProvider

About the 2 providers, the only thing they share in common is the name and the ability to inject the theme object to the children.关于这 2 个提供者,他们唯一的共同点是名称和将主题对象注入孩子的能力。 ThemeProvider from emotion can accept any theme object and is not integrated well with the MUI APIs as seen from the other answer here , because it's only meant to use with emotion alone. ThemeProvider从情感可以接受任何theme的对象,而不是与MUI的API集成以及从对方的回答看到这里,因为它只有单独意味着使用感慨。

The one from MUI has a stricter type and is used by most MUI components in the framework, so if you change one variable in MUI theme, many MUI components will be affected, it also has the default theme if you don't provide any. MUI中的那个类型比较严格,框架中大部分MUI组件都使用,所以如果你改变MUI主题中的一个变量,会影响很多MUI组件,如果你不提供,它也有默认主题

So if you only use emotion without MUI and you want to share some styles across the children, use emotion's ThemeProvider .因此,如果您只使用没有 MUI 的情绪,并且想在孩子之间共享一些样式,请使用情绪的ThemeProvider Since you're using MUI just ignore it, it's not relevant here.由于您使用的是 MUI,因此请忽略它,因此此处无关紧要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM