How to make the GridList component in React Material-UI responsive
I'm using the Material-UI version 1 to make the grid UI in my React application. I want to make the grid responsive. The GridList component API has cols props, which by default is 2.
For example, it could looks like this below.
<GridList cols={1} spacing={32} className="list"></GridList>
Can I dynamically change the props? Or are there any other ways to make it responsive?
reactjs responsive material-ui
add a comment |
I'm using the Material-UI version 1 to make the grid UI in my React application. I want to make the grid responsive. The GridList component API has cols props, which by default is 2.
For example, it could looks like this below.
<GridList cols={1} spacing={32} className="list"></GridList>
Can I dynamically change the props? Or are there any other ways to make it responsive?
reactjs responsive material-ui
add a comment |
I'm using the Material-UI version 1 to make the grid UI in my React application. I want to make the grid responsive. The GridList component API has cols props, which by default is 2.
For example, it could looks like this below.
<GridList cols={1} spacing={32} className="list"></GridList>
Can I dynamically change the props? Or are there any other ways to make it responsive?
reactjs responsive material-ui
I'm using the Material-UI version 1 to make the grid UI in my React application. I want to make the grid responsive. The GridList component API has cols props, which by default is 2.
For example, it could looks like this below.
<GridList cols={1} spacing={32} className="list"></GridList>
Can I dynamically change the props? Or are there any other ways to make it responsive?
reactjs responsive material-ui
reactjs responsive material-ui
asked Feb 22 '18 at 7:04
sflowsflow
6819
6819
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property.
The code will look like this:
...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...
const MyComponent = props => {
const getGridListCols = () => {
if (isWidthUp('xl', props.width)) {
return 4;
}
if (isWidthUp('lg', props.width)) {
return 3;
}
if (isWidthUp('md', props.width)) {
return 2;
}
return 1;
}
return(
...
<GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
{
myItemsArray.map(item => (
<GridListTile key={item} cols={1}>
<FooBar />
</GridListTile>
))
}
</GridList>
...
);
};
...
export default withWidth()(MyComponent);
add a comment |
You can choose the cols value depending on the width (xs, sm, md, lg, xl), look at this example where 1 column is set for smaller screens.
import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
const styles = theme => ({ });
class MyComponent extends React.Component {
render() {
const { classes, currentUser, images, width } = this.props;
let columns = width === 'xs' || width === 'sm' ? 1 : 2;
return (
<div className={classes.root}>
<GridList cellHeight={180} cols={columns} className={classes.gridList}>
{images.map(image => (
<GridListTile key={image.id}>
<img src={ image.path } />
<GridListTileBar
title={ image.name }
/>
</GridListTile>
))}
</GridList>
</div>
);
}
}
MyComponent.propTypes = {
currentUser: PropTypes.object,
images: PropTypes.array.isRequired
};
function mapStateToProps(state) {
return {
currentUser: state.user.currentUser
};
}
export default compose(
withStyles(styles, {
name: 'MyComponent',
}),
withWidth(),
connect(mapStateToProps),
)(MyComponent);
add a comment |
When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started.
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48921432%2fhow-to-make-the-gridlist-component-in-react-material-ui-responsive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property.
The code will look like this:
...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...
const MyComponent = props => {
const getGridListCols = () => {
if (isWidthUp('xl', props.width)) {
return 4;
}
if (isWidthUp('lg', props.width)) {
return 3;
}
if (isWidthUp('md', props.width)) {
return 2;
}
return 1;
}
return(
...
<GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
{
myItemsArray.map(item => (
<GridListTile key={item} cols={1}>
<FooBar />
</GridListTile>
))
}
</GridList>
...
);
};
...
export default withWidth()(MyComponent);
add a comment |
U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property.
The code will look like this:
...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...
const MyComponent = props => {
const getGridListCols = () => {
if (isWidthUp('xl', props.width)) {
return 4;
}
if (isWidthUp('lg', props.width)) {
return 3;
}
if (isWidthUp('md', props.width)) {
return 2;
}
return 1;
}
return(
...
<GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
{
myItemsArray.map(item => (
<GridListTile key={item} cols={1}>
<FooBar />
</GridListTile>
))
}
</GridList>
...
);
};
...
export default withWidth()(MyComponent);
add a comment |
U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property.
The code will look like this:
...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...
const MyComponent = props => {
const getGridListCols = () => {
if (isWidthUp('xl', props.width)) {
return 4;
}
if (isWidthUp('lg', props.width)) {
return 3;
}
if (isWidthUp('md', props.width)) {
return 2;
}
return 1;
}
return(
...
<GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
{
myItemsArray.map(item => (
<GridListTile key={item} cols={1}>
<FooBar />
</GridListTile>
))
}
</GridList>
...
);
};
...
export default withWidth()(MyComponent);
U can use the layout breakpoints provide by MUI to toggle GridList or GridListTile cols property.
The code will look like this:
...
import withWidth, { isWidthUp } from '@material-ui/core/withWidth';
...
const MyComponent = props => {
const getGridListCols = () => {
if (isWidthUp('xl', props.width)) {
return 4;
}
if (isWidthUp('lg', props.width)) {
return 3;
}
if (isWidthUp('md', props.width)) {
return 2;
}
return 1;
}
return(
...
<GridList spacing={15} cellHeight={400} cols={getGridListCols()}>
{
myItemsArray.map(item => (
<GridListTile key={item} cols={1}>
<FooBar />
</GridListTile>
))
}
</GridList>
...
);
};
...
export default withWidth()(MyComponent);
answered Nov 25 '18 at 0:50
Morlo MbakopMorlo Mbakop
7915
7915
add a comment |
add a comment |
You can choose the cols value depending on the width (xs, sm, md, lg, xl), look at this example where 1 column is set for smaller screens.
import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
const styles = theme => ({ });
class MyComponent extends React.Component {
render() {
const { classes, currentUser, images, width } = this.props;
let columns = width === 'xs' || width === 'sm' ? 1 : 2;
return (
<div className={classes.root}>
<GridList cellHeight={180} cols={columns} className={classes.gridList}>
{images.map(image => (
<GridListTile key={image.id}>
<img src={ image.path } />
<GridListTileBar
title={ image.name }
/>
</GridListTile>
))}
</GridList>
</div>
);
}
}
MyComponent.propTypes = {
currentUser: PropTypes.object,
images: PropTypes.array.isRequired
};
function mapStateToProps(state) {
return {
currentUser: state.user.currentUser
};
}
export default compose(
withStyles(styles, {
name: 'MyComponent',
}),
withWidth(),
connect(mapStateToProps),
)(MyComponent);
add a comment |
You can choose the cols value depending on the width (xs, sm, md, lg, xl), look at this example where 1 column is set for smaller screens.
import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
const styles = theme => ({ });
class MyComponent extends React.Component {
render() {
const { classes, currentUser, images, width } = this.props;
let columns = width === 'xs' || width === 'sm' ? 1 : 2;
return (
<div className={classes.root}>
<GridList cellHeight={180} cols={columns} className={classes.gridList}>
{images.map(image => (
<GridListTile key={image.id}>
<img src={ image.path } />
<GridListTileBar
title={ image.name }
/>
</GridListTile>
))}
</GridList>
</div>
);
}
}
MyComponent.propTypes = {
currentUser: PropTypes.object,
images: PropTypes.array.isRequired
};
function mapStateToProps(state) {
return {
currentUser: state.user.currentUser
};
}
export default compose(
withStyles(styles, {
name: 'MyComponent',
}),
withWidth(),
connect(mapStateToProps),
)(MyComponent);
add a comment |
You can choose the cols value depending on the width (xs, sm, md, lg, xl), look at this example where 1 column is set for smaller screens.
import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
const styles = theme => ({ });
class MyComponent extends React.Component {
render() {
const { classes, currentUser, images, width } = this.props;
let columns = width === 'xs' || width === 'sm' ? 1 : 2;
return (
<div className={classes.root}>
<GridList cellHeight={180} cols={columns} className={classes.gridList}>
{images.map(image => (
<GridListTile key={image.id}>
<img src={ image.path } />
<GridListTileBar
title={ image.name }
/>
</GridListTile>
))}
</GridList>
</div>
);
}
}
MyComponent.propTypes = {
currentUser: PropTypes.object,
images: PropTypes.array.isRequired
};
function mapStateToProps(state) {
return {
currentUser: state.user.currentUser
};
}
export default compose(
withStyles(styles, {
name: 'MyComponent',
}),
withWidth(),
connect(mapStateToProps),
)(MyComponent);
You can choose the cols value depending on the width (xs, sm, md, lg, xl), look at this example where 1 column is set for smaller screens.
import React from "react";
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
// @material-ui/core components
import withStyles from "@material-ui/core/styles/withStyles";
import withWidth from '@material-ui/core/withWidth';
import GridList from '@material-ui/core/GridList';
import GridListTile from '@material-ui/core/GridListTile';
import GridListTileBar from '@material-ui/core/GridListTileBar';
const styles = theme => ({ });
class MyComponent extends React.Component {
render() {
const { classes, currentUser, images, width } = this.props;
let columns = width === 'xs' || width === 'sm' ? 1 : 2;
return (
<div className={classes.root}>
<GridList cellHeight={180} cols={columns} className={classes.gridList}>
{images.map(image => (
<GridListTile key={image.id}>
<img src={ image.path } />
<GridListTileBar
title={ image.name }
/>
</GridListTile>
))}
</GridList>
</div>
);
}
}
MyComponent.propTypes = {
currentUser: PropTypes.object,
images: PropTypes.array.isRequired
};
function mapStateToProps(state) {
return {
currentUser: state.user.currentUser
};
}
export default compose(
withStyles(styles, {
name: 'MyComponent',
}),
withWidth(),
connect(mapStateToProps),
)(MyComponent);
answered Nov 24 '18 at 21:27
Felipe PereiraFelipe Pereira
5,31222535
5,31222535
add a comment |
add a comment |
When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started.
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
add a comment |
When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started.
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
add a comment |
When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started.
When you say you want to make the grid responsive, do you perhaps mean adding more items dynamically? If not, by default Material-UI is responsive (meaning your viewport is dynamically sized depending on the device you view on), and depending on your GridList properties (like cols, rows, spacing etc) you can determine the style. If you have a look at https://material-ui-next.com/demos/grid-list/ there are a few examples to get you started.
answered Apr 25 '18 at 13:40
NitaNita
12011
12011
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
add a comment |
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
@sflow did you find the above helpful in solving your problem?
– Nita
Jun 14 '18 at 10:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48921432%2fhow-to-make-the-gridlist-component-in-react-material-ui-responsive%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown