question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

[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?

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 from react, why should imports from material-ui be different?
  • 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 pure Mui button, whereas Button is part of your projectā€™s modifications that fell outside of what you can do with themeā€™s customizations
  • itā€™s better marketing for material-ui and useful for beginners, as developers are reminded on what they use as they use values from Mui.

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): Screen Shot 2020-09-08 at 4 21 39 PM

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:25 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
oliviertassinaricommented, Sep 12, 2020

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, not const { divider } = theme.palette.

Itā€™s important for refactoring.

2reactions
troglotitcommented, Feb 4, 2021

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

import * as MUI from '@material-ui/core'
import * as C from '../components'
// components/index.js
export * from 'Button.js'

compared to

import { Button as MuiButton } from '@material-ui/core'
import { Button as MyButton } from '../components/Button.js' // or import Button from '../components/Button.js'

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found