[RFC] Use `import * as Mui from 'material-ui'` in the demos
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
Summary š”
Instead of recommending to use named exports, how about recommending importing all modules?
- it wonāt increase the bundles and is not worse in terms of initial parsing for development workflow than named imports
Examples š
Before:
import { Button, Table, ... } from "@material-ui/core"
After:
import * as Mui from "@material-ui/core"
Motivation š¦
Pros:
- improves consistency
- because a similar decision was made here
material-ui
doesnāt import named exports fromreact
, why should imports frommaterial-ui
be different?
- because a similar decision was made here
- improves DX
- as it removes the need to manually clean up unused imports (when there is a linter rule in development workflow).
- as it removes the need to manually add new imports from
Mui
- as IDEs most of the time fail to auto import from libraries already present in the module
- as it is cleaner, since less individual imports are in the module
- it is easier to understand where the variable is coming from while looking at the code, as you know the variable in question belongs to
Mui
- for example:
Mui.Button
is a pureMui
button, whereasButton
is part of your projectās modifications that fell outside of what you can do with themeās customizations
- for example:
- it is easier to understand where the variable is coming from while looking at the code, as you know the variable in question belongs to
- itās better marketing for
material-ui
and useful for beginners, as developers are reminded on what they use as they use values fromMui.
Cons:
- the amount of code writing (less lines, but a bit more actual text) is more as thereād be a prefix everywhere:
Mui.
- but the clear indication where these variables are coming from is far more important
Example project
For your convenience I created a simple example that showcases:
- tree-shaking of
material-ui
works when importing all
Basically I put @oliviertassinari example function into a new CRA project. Please follow commits as itās easier to understand what has been changed in a pure CRA project.
And here is a screenshot of the webpack-bundle-analyzer. We can clearly see that only the button and whatās necessary for it is imported (to see it for yourself, run yarn build
):
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:25 (10 by maintainers)
Top Results From Across the Web
Usage - Material UI - MUI
The following code snippet demonstrates a simple app that uses the Material UI Button component: import * as React from 'react'; import Button...
Read more >Introducing MUI Core v5.0
You can find the initial RFC plan for v5 in issue #20012. A new brand. Material UI is now MUI! Head to the...
Read more >Understanding MUI packages - Material UI
If you want to build a design system based on Material Design, use @mui/material . Ā· If you want to build with components...
Read more >@mui/material | Yarn - Package Manager
React components that implement Google's Material Design. ... MUI Core contains foundational React UI component libraries for shipping new features faster.
Read more >Rendering Material-UI icons from an array - Stack Overflow
To use an icon simply wrap the icon name (font ligature) with the Icon component, for example: import Icon from '@material-ui/core/Icon';Ā ...
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 FreeTop 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
Top GitHub Comments
Another pros I didnāt thought about before. The mui.* pattern makes it easier to identify whatās used in the codebase. Say you have multiple custom buttons, searching for
mui.Button
will give you more accurate results.We never destruture the theme object in the codebase for this very reason. We always do
theme.palette.divider
, notconst { divider } = theme.palette
.Itās important for refactoring.
My experience report as per https://twitter.com/troglotit/status/1356496801067589633 is that itās a bit different paradigm, bit it shines when you structure your app using namespaces/modules
compared to
Some can say that one-letter variables are bad, but when for the whole app only components-module is named āCā itās like using āiā in for-loops. And IntelliSense with namespaces/modules is so satisfying
Then I free myself from problem of either a) name-clashes of auto-import (it just seems bizarre that only yesterday we stopped using global scoping, but somehow itās ok for auto-imports) b) coming up with āMyButtonā name when itās just a regular button in my app code.