Best practice of setting props depending on breakpoints
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 99.1k
- Forks
- 32.5k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 106
Description
Discussed in https://github.com/mui-org/material-ui/discussions/29318
Originally posted by mwskwong October 27, 2021
For example, I want to display a Button and set fullWidth={true} while displaying in mobile devices (i.e. smDown) and false if otherwise. I can think of two ways to implement it.
-
Use
useMediaQuery. The pros are the code will be more elegant, while the issue is it doesn't work very well with SSR. Youn will notice a layout shift in certain cases. -
Mount two
Button, one withfullWidthset totrueand the other one set tofalse, and then set the CSS attributedisplaydepending on the breakpoint. The pros are it supports SSR very well., While the cons are you have to duplicate the component, which is not so great for performance and SEO.
Now the question is, which one do you prefer? Or are there better ways to achieve this?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked discussion and compare the two approaches described: useMediaQuery with SSR versus mounting two Button components with breakpoint-based display. Determine whether the desired outcome is a documented recommendation or a new responsive API, then define acceptance criteria for the chosen approach and its SSR behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100