How to make the GridList component in React Material-UI responsive












2















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?










share|improve this question



























    2















    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?










    share|improve this question

























      2












      2








      2








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Feb 22 '18 at 7:04









      sflowsflow

      6819




      6819
























          3 Answers
          3






          active

          oldest

          votes


















          3














          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);





          share|improve this answer































            1














            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);





            share|improve this answer































              0














              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.






              share|improve this answer
























              • @sflow did you find the above helpful in solving your problem?

                – Nita
                Jun 14 '18 at 10:02











              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
              });


              }
              });














              draft saved

              draft discarded


















              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









              3














              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);





              share|improve this answer




























                3














                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);





                share|improve this answer


























                  3












                  3








                  3







                  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);





                  share|improve this answer













                  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);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 0:50









                  Morlo MbakopMorlo Mbakop

                  7915




                  7915

























                      1














                      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);





                      share|improve this answer




























                        1














                        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);





                        share|improve this answer


























                          1












                          1








                          1







                          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);





                          share|improve this answer













                          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);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 24 '18 at 21:27









                          Felipe PereiraFelipe Pereira

                          5,31222535




                          5,31222535























                              0














                              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.






                              share|improve this answer
























                              • @sflow did you find the above helpful in solving your problem?

                                – Nita
                                Jun 14 '18 at 10:02
















                              0














                              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.






                              share|improve this answer
























                              • @sflow did you find the above helpful in solving your problem?

                                – Nita
                                Jun 14 '18 at 10:02














                              0












                              0








                              0







                              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.






                              share|improve this answer













                              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.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              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



















                              • @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


















                              draft saved

                              draft discarded




















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              Popular posts from this blog

                              Lallio

                              Unable to find Lightning Node

                              Futebolista